xml的dom解析思想如下图
dom解析思想:得到文档节点通过节点往下搜寻,它就是一棵树。
下面是dom4j的入门操作。
(1)解析xml文档
public Document parse(URL url) throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(url); return document; }
(2)遍历xml的元素,使用迭代器
public void bar(Document document) throws DocumentException { Element root = document.getRootElement(); // iterate through child elements of root for ( Iterator i = root.elementIterator(); i.hasNext(); ) { Element element = (Element) i.next(); // do something } // iterate through child elements of root with element name "foo" for ( Iterator i = root.elementIterator( "foo" ); i.hasNext(); ) { Element foo = (Element) i.next(); // do something } // iterate through attributes of root for ( Iterator i = root.attributeIterator(); i.hasNext(); ) { Attribute attribute = (Attribute) i.next(); // do something }
(3)快速遍历
public void treeWalk(Document document) { treeWalk( document.getRootElement() ); } public void treeWalk(Element element) { for ( int i = 0,size = element.nodeCount(); i < size; i++ ) { Node node = element.node(i); if ( node instanceof Element ) { treeWalk( (Element) node ); } else { // do something.... } }
(4)创建xml文件
public Document createDocument() { Document document = DocumentHelper.createDocument(); Element root = document.addElement( "root" ); Element author1 = root.addElement( "author" ) .addAttribute( "name","James" ) .addAttribute( "location","UK" ) .addText( "James Strachan" ); Element author2 = root.addElement( "author" ) .addAttribute( "name","Bob" ) .addAttribute( "location","US" ) .addText( "Bob McWhirter" ); return document; }
(5)将xml文件写入磁盘
public void write(Document document) throws IOException { // lets write to a file XMLWriter writer = new XMLWriter( new FileWriter( "output.xml" ) ); writer.write( document ); writer.close(); // Pretty print the document to System.out OutputFormat format = OutputFormat.createPrettyPrint(); writer = new XMLWriter( System.out,format ); writer.write( document ); // Compact format to System.out format = OutputFormat.createCompactFormat(); writer = new XMLWriter( System.out,format ); writer.write( document ); }
(6)修改后的dom树更新到内存中去
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"),OutputFormat.createPrettyPrint()); writer.write( document ); writer.close();
以book.xml为例
<?xml version="1.0" encoding="UTF-8"?> <书架> <书> <书名>Java就业培训教程</书名> <作者>小凌</作者> <售价>39.00元</售价> <特价>20</特价> </书> <书> <书名>JavaScript网页开发</书名> <作者>张孝祥</作者> <售价>28.00元</售价> </书> </书架>
java代码
/** * 解析xml文件 * @throws DocumentException */ public void ParsingXml() throws DocumentException { SAXReader reader = new SAXReader(); Document doc = reader.read("book.xml"); Element root = doc.getRootElement(); //遍历xml的元素 for (Iterator i = root.elementIterator(); i.hasNext();) { Element Root = (Element) i.next(); String titile = Root.element("书名").getText(); String author = Root.element("作者").getText(); String price = Root.element("售价").getText(); System.out.println(titile+":"+author+":"+price); } }
例子二:xml增删改查,以上面的book.xml为基础
public void add() throws DocumentException,IOException{ SAXReader reader = new SAXReader(); Document doc = reader.read("book.xml"); Element root = doc.getRootElement(); //得到根节点 Element price = DocumentHelper.createElement( "特价" ); price.setText("20"); Element book = root.element("书"); book.add(price); XMLWriter writer = new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint()); writer.write( doc ); writer.close(); } public void del() throws DocumentException,IOException { SAXReader reader = new SAXReader(); Document doc = reader.read("output.xml"); Element root = doc.getRootElement(); Element e = root.element("书").element("特价"); e.getParent().remove(e); //更新内存 XMLWriter writer = new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint()); writer.write( doc ); writer.close(); } public void updata() throws DocumentException,IOException{ SAXReader reader = new SAXReader(); Document doc = reader.read("book.xml"); Element root = doc.getRootElement(); root.element("书").element("作者").setText("小凌"); //更新内存 XMLWriter writer = new XMLWriter(new FileOutputStream("book.xml"),OutputFormat.createPrettyPrint()); writer.write( doc ); writer.close(); }原文链接:https://www.f2er.com/xml/296688.html