java读取XML文件的四种方法总结(必看篇)
程序员文章站
2023-12-19 13:37:52
java操作xml文档主要有四种方式,分别是dom、sax、jdom和dom4j,dom和sax是官方提供的,而jdom和dom4j则是引用第三方库的,其中用的最多的是do...
java操作xml文档主要有四种方式,分别是dom、sax、jdom和dom4j,dom和sax是官方提供的,而jdom和dom4j则是引用第三方库的,其中用的最多的是dom4j方式。运行效率和内存使用方面最优的是sax,但是由于sax是基于事件的方式,所以sax无法在编写xml的过程中对已编写内容进行修改,但对于不用进行频繁修改的需求,还是应该选择使用sax。
下面基于这四种方式来读取xml文件。
第一,以dom的方式实现。
package xmls; import org.w3c.dom.document; import org.w3c.dom.element; import org.w3c.dom.node; import org.w3c.dom.nodelist; import org.xml.sax.saxexception; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception; import java.io.file; import java.io.ioexception; /** * created by lenovo on 2017-6-3. */ public class domreaddemo { public static void main(string[] args){ documentbuilderfactory dbf = documentbuilderfactory.newinstance(); try{ documentbuilder db = dbf.newdocumentbuilder(); document document = db.parse("src/xmls/dom.xml"); nodelist booklist = document.getelementsbytagname("book"); for(int i = 0; i < booklist.getlength(); i++){ system.out.println("--------第" + (i+1) + "本书----------"); element ele = (element) booklist.item(i); nodelist childnodes= ele.getchildnodes(); for(int j = 0; j < childnodes.getlength(); j++){ node n = childnodes.item(j); if(n.getnodename() != "#text"){ system.out.println(n.getnodename() + ":" + n.gettextcontent()); } } system.out.println("---------------------------------"); } }catch (parserconfigurationexception e){ e.printstacktrace(); }catch (ioexception e){ e.printstacktrace(); }catch (saxexception e){ e.printstacktrace(); } } }
第二,以sax的方式实现。
package xmls; import javax.xml.parsers.saxparser; import javax.xml.parsers.saxparserfactory; /** * created by lenovo on 2017-6-1. */ public class xmltest2 { public static void main(string[] args){ saxparserfactory spf = saxparserfactory.newinstance(); try{ saxparser sp = spf.newsaxparser(); saxparserhandler handler = new saxparserhandler(); sp.parse("src\\xmls\\book.xml", handler); }catch (exception e){ e.printstacktrace(); } } }
package xmls; import org.xml.sax.attributes; import org.xml.sax.saxexception; import org.xml.sax.helpers.defaulthandler; /** * created by lenovo on 2017-6-1. */ public class saxparserhandler extends defaulthandler { @override public void startdocument() throws saxexception { super.startdocument(); system.out.println("sax解析开始"); } @override public void enddocument() throws saxexception { super.enddocument(); system.out.println("sax解析结束"); } @override public void startelement(string s, string s1, string s2, attributes attributes) throws saxexception { super.startelement(s, s1, s2, attributes); system.out.println(s2); for(int i = 0; i < attributes.getlength(); i++){ string name = attributes.getqname(i); string value = attributes.getvalue(name); system.out.println("属性值:" + name + "=" + value); } } @override public void endelement(string s, string s1, string s2) throws saxexception { super.endelement(s, s1, s2); if(s2.equals("book")){ system.out.println("-----------------------"); } } @override public void characters(char[] ch, int start, int length) throws saxexception { super.characters(ch, start, length); string value = new string(ch, start, length); if(value.trim().equals("")){ return; } system.out.println(value); } }
第三,以jdom的方式实现。
package xmls; import org.jdom2.attribute; import org.jdom2.document; import org.jdom2.element; import org.jdom2.jdomexception; import org.jdom2.input.jdomparseexception; import org.jdom2.input.saxbuilder; import java.io.*; import java.util.list; /** * created by lenovo on 2017-6-2. */ public class jdomtest { public static void main(string[] args){ saxbuilder saxbuilder = new saxbuilder(); inputstream in; try{ in = new fileinputstream(new file("src\\xmls\\book.xml")); document document = saxbuilder.build(in); element rootelement = document.getrootelement(); list<element> booklist = rootelement.getchildren(); for(element book: booklist){ system.out.println("第" + (booklist.indexof(book)+1) + "本书!"); list<attribute> attrs = book.getattributes(); for(attribute attr: attrs){ system.out.println(attr.getname() + "=" + attr.getvalue()); } for(element item: book.getchildren()){ system.out.println(item.getname() + ":" + item.getvalue()); } system.out.println("------------------------------------"); } }catch (filenotfoundexception e){ e.printstacktrace(); }catch (jdomexception e){ e.printstacktrace(); }catch (ioexception e){ e.printstacktrace(); } } }
第四,以dom4j的方式实现。
package xmls; import org.dom4j.*; import org.dom4j.io.outputformat; import org.dom4j.io.saxreader; import org.dom4j.io.xmlwriter; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.util.iterator; import java.util.list; /** * created by lenovo on 2017-6-2. */ public class dom4jtest { public void parsexml(){ saxreader saxreader = new saxreader(); try{ document document = saxreader.read(new file("src\\xmls\\book.xml")); element rootelement = document.getrootelement(); iterator it = rootelement.elementiterator(); while (it.hasnext()){ element book = (element)it.next(); list<attribute> attrs = book.attributes(); for(attribute attr: attrs){ system.out.println("属性名:" + attr.getname() + "---- 属性值:" + attr.getvalue() ); } iterator cit = book.elementiterator(); while (cit.hasnext()){ element child = (element) cit.next(); system.out.println("子节点:" + child.getname()); } } }catch (documentexception e){ e.printstacktrace(); } } public static void main(string[] args){ dom4jtest dom4jtest = new dom4jtest(); dom4jtest.parsexml(); } }
以上这篇java读取xml文件的四种方法总结(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。