XML JAVA解析 -- JDOM
oop 解析 xml 的java实现。
?
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.jdom.attribute;<br>import org.jdom.content;<br>import org.jdom.document;<br>import org.jdom.element;<br>import org.jdom.jdomexception;<br>import org.jdom.text;<br>import org.jdom.input.dombuilder;<br>import org.jdom.input.saxbuilder;<br>import org.jdom.output.xmloutputter;
public class jdomtest {<br>?<br>?public static void printelement(element e) {<br>??system.out.println("element : " + e.getname() + "---------------");<br>??list attributes = e.getattributes();<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>??list contents = e.getcontent();<br>??iterator iter = contents.iterator();<br>??while (iter.hasnext()) {<br>???content content = (content) iter.next();<br>???if (content instanceof element) {<br>????printelement((element) content);<br>???} else if (content instanceof text) {<br>????system.out.println(((text) content).gettext());<br>???}<br>??}<br>?}<br>?<br>?/**<br>? * @param args<br>? * @throws ioexception <br>? * @throws jdomexception <br>? */<br>?public static void main(string[] args) throws jdomexception, ioexception {<br>??// todo auto-generated method stub<br>??saxbuilder builder = new saxbuilder();<br>??builder.setvalidation(true);<br>??document document = builder.build("src/com/siyuan/xml/workers.xml");<br>??printelement(document.getrootelement());<br>??<br>??xmloutputter transformer = new xmloutputter();<br>??transformer.output(document, new fileoutputstream(new file("src/com/siyuan/xml/workers2.xml")));<br>?}
}