欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JDOM解析XML简单应用

程序员文章站 2022-01-20 10:05:53
...
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class StoryXml {
	/**
	 * 查询ID是否存在
	 * @param path xml文件绝对路径
	 * @param id 节点id
	 * @return
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	@SuppressWarnings("unchecked")
	public static boolean findElementByIdInXml(String path, String id) throws JDOMException, IOException {
		SAXBuilder saxbuilder = new SAXBuilder();
		Document doc = saxbuilder.build(new File(path));
		Element root = doc.getRootElement();
		List<Element> list = root.getChildren();
		for (Element e : list) {
			if(e.getAttributeValue("id").equalsIgnoreCase(id)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 更新指定ID的XML
	 * @param path xml文件绝对路径
	 * @param id 节点id
	 * @param imgpath 新图片路径
	 * @param description 图片描述
	 * @throws JDOMException
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public static void updateElementById(String path, String id, String imgpath, String description) throws JDOMException, IOException {
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(new File(path));
		Element element = doc.getRootElement();
		List<Element> list = element.getChildren();
		Element image = null;
		Element des = null;
		for(Element e : list){
			if(e.getAttributeValue("id").equalsIgnoreCase(id)) {
				image = e.getChild("image");
				image.setText(imgpath);
				des = e.getChild("description");
				des.setText(description);
			}
		}
		XMLOutputter out = new XMLOutputter();
		out.setFormat(Format.getPrettyFormat().setEncoding("utf-8"));
		out.output(doc, new FileOutputStream(path));
	}

	/**
	 * 删除指定编号的节点
	 * @param path xml文件路径
	 * @param id 结点id
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	@SuppressWarnings("unchecked")
	public static void removeElement(String path, String id) throws JDOMException, IOException {
		SAXBuilder builder = new SAXBuilder();
		Document doc =  builder.build(new File(path));;
		Element root=doc.getRootElement();
		List<Element> childs = root.getChildren();
		for(Element e : childs){
			if(id.equalsIgnoreCase(e.getAttributeValue("id"))){
				root.removeContent(e);
				break;
			}
		}
		XMLOutputter out = new XMLOutputter();
		out.setFormat(Format.getPrettyFormat().setEncoding("utf-8"));
		out.output(doc, new FileOutputStream(path));
	}

	/**
	 * 创建新节点
	 * @param path
	 * @param id
	 * @param imgpath
	 * @param description
	 * @throws IOException 
	 * @throws JDOMException 
	 */
	public static void createElement(String path, String id, String imgpath, String description) throws JDOMException, IOException {
		Element fish_node = new Element("thumb");
		Element fish_node_image = new Element("image");
		Element fish_node_description = new Element("description");
		fish_node_image.addContent(imgpath);
		fish_node_description.addContent(description);
		fish_node.addContent(0, fish_node_image);
		fish_node.addContent(1, fish_node_description);
		fish_node.setAttribute("id", id);
		fish_node.setAttribute("displayNum", "10");
		fish_node.setAttribute("separation", "5");
		
		SAXBuilder builder = new SAXBuilder();
		Document doc =  builder.build(new File(path));
		Element root=doc.getRootElement();
		root.addContent(fish_node);
		XMLOutputter out = new XMLOutputter();
		out.setFormat(Format.getPrettyFormat().setEncoding("utf-8"));
		out.output(doc, new FileOutputStream(path));
	}
}

 

 

相关标签: XML