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));
}
}
上一篇: SAX 解析大型XML文件
推荐阅读
-
解析iOS应用的UI开发中懒加载和xib的简单使用方法
-
Python通过DOM和SAX方式解析XML的应用实例分享
-
Python 解析简单的XML数据
-
Jquery Ajax解析XML数据(同步及异步调用)简单实例
-
java xml解析方式(DOM、SAX、JDOM、DOM4J)
-
Python爬虫10-页面解析数据提取思路方法与简单正则应用
-
Java程序员从笨鸟到菜鸟之(二十七)XML之Jdom和DOM4J解析 .
-
XML--jdom/dom4j/sax解析XML文件
-
Java基础之XML介绍与SAX解析、DOM解析XML、JDOM解析、DOM4J解析、XMLEncoder与XMLDecoder的使用以及xstream工具的使用 189~195
-
day0807(xml原理、dom解析、sax解析、jdom解析、dom4j解析、工具类书写)