XML JAVA解析 -- DOM4J

    1. 实例
 
package com.siyuan.xml;
import java.io.file;<br>import java.io.fileoutputstream;<br>import java.io.ioexception;<br>import java.util.iterator;<br>import java.util.list;
import org.dom4j.attribute;<br>import org.dom4j.document;<br>import org.dom4j.documentexception;<br>import org.dom4j.element;<br>import org.dom4j.io.saxreader;<br>import org.dom4j.io.xmlwriter;
public class dom4jtest {<br> <br> public static void printelement(element e) {<br>  system.out.println("element : " + e.getname() + "---------------");<br>  <br>  list attributes = e.attributes();<br>  iterator iterattrs = attributes.iterator();<br>  while (iterattrs.hasnext()) {<br>   attribute attri = (attribute) iterattrs.next();<br>   system.out.println("attribute " + attri.getname() + "=" + attri.getvalue());<br>  }<br>  <br>  system.out.println(e.gettext());<br>  <br>  list children = e.elements();<br>  iterator iter = children.iterator();<br>  while (iter.hasnext()) {<br>   printelement((element) iter.next());<br>  }<br> }<br> <br> /**<br>  * @param args<br>  * @throws documentexception <br>  * @throws ioexception <br>  */<br> public static void main(string[] args) throws documentexception, ioexception {<br>  // todo auto-generated method stub<br>  saxreader reader = new saxreader();<br>  reader.setvalidation(true);<br>  document doc = reader.read("src/com/siyuan/xml/workers.xml");<br>  printelement(doc.getrootelement());<br>  <br>  xmlwriter writer = new xmlwriter(new fileoutputstream(new file("src/com/siyuan/xml/workers3.xml")));<br>  writer.write(doc);<br>  writer.close();<br> }
}
 

你可能感兴趣的:(java,工作)