java解析XML几种方式小结
java解析xml几种方式小结
第一种:dom。
dom的全称是document object model,也即文档对象模型。在应用程序中,基于dom的xml分析器将一个xml文档转换成一个对象模型的集合(通常称dom树),应用程序正是通过对这个对象模型的操作,来实现对xml文档数据的操作。通过dom接口,应用程序可以在任何时候访问xml文档中的任何一部分数据,因此,这种利用dom接口的机制也被称作随机访问机制。
dom接口提供了一种通过分层对象模型来访问xml文档信息的方式,这些分层对象模型依据xml的文档结构形成了一棵节点树。无论xml文档中所描述的是什么类型的信息,即便是制表数据、项目列表或一个文档,利用dom所生成的模型都是节点树的形式。也就是说,dom强制使用树模型来访问xml文档中的信息。由于xml本质上就是一种分层结构,所以这种描述方法是相当有效的。
dom树所提供的随机访问方式给应用程序的开发带来了很大的灵活性,它可以任意地控制整个xml文档中的内容。然而,由于dom分析器把整个xml文档转化成dom树放在了内存中,因此,当文档比较大或者结构比较复杂时,对内存的需求就比较高。而且,对于结构复杂的树的遍历也是一项耗时的操作。所以,dom分析器对机器性能的要求比较高,实现效率不十分理想。不过,由于dom分析器所采用的树结构的思想与xml文档的结构相吻合,同时鉴于随机访问所带来的方便,因此,dom分析器还是有很广泛的使用价值的。
import java.io.file; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import org.w3c.dom.document; import org.w3c.dom.element; import org.w3c.dom.nodelist; public class domtest1 { public static void main(string[] args) throws exception { // step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器) documentbuilderfactory dbf = documentbuilderfactory.newinstance(); // system.out.println("class name: " + dbf.getclass().getname()); // step 2:获得具体的dom解析器 documentbuilder db = dbf.newdocumentbuilder(); // system.out.println("class name: " + db.getclass().getname()); // step3: 解析一个xml文档,获得document对象(根结点) document document = db.parse(new file("candidate.xml")); nodelist list = document.getelementsbytagname("person"); for(int i = 0; i < list.getlength(); i++) { element element = (element)list.item(i); string content = element.getelementsbytagname("name").item(0).getfirstchild().getnodevalue(); system.out.println("name:" + content); content = element.getelementsbytagname("address").item(0).getfirstchild().getnodevalue(); system.out.println("address:" + content); content = element.getelementsbytagname("tel").item(0).getfirstchild().getnodevalue(); system.out.println("tel:" + content); content = element.getelementsbytagname("fax").item(0).getfirstchild().getnodevalue(); system.out.println("fax:" + content); content = element.getelementsbytagname("email").item(0).getfirstchild().getnodevalue(); system.out.println("email:" + content); system.out.println("--------------------------------------"); } } }
import java.io.file; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import org.w3c.dom.attr; import org.w3c.dom.comment; import org.w3c.dom.document; import org.w3c.dom.element; import org.w3c.dom.namednodemap; import org.w3c.dom.node; import org.w3c.dom.nodelist; /** * 使用递归解析给定的任意一个xml文档并且将其内容输出到命令行上 * @author zhanglong * */ public class domtest3 { public static void main(string[] args) throws exception { documentbuilderfactory dbf = documentbuilderfactory.newinstance(); documentbuilder db = dbf.newdocumentbuilder(); document doc = db.parse(new file("student.xml")); //获得根元素结点 element root = doc.getdocumentelement(); parseelement(root); } private static void parseelement(element element) { string tagname = element.getnodename(); nodelist children = element.getchildnodes(); system.out.print("<" + tagname); //element元素的所有属性所构成的namednodemap对象,需要对其进行判断 namednodemap map = element.getattributes(); //如果该元素存在属性 if(null != map) { for(int i = 0; i < map.getlength(); i++) { //获得该元素的每一个属性 attr attr = (attr)map.item(i); string attrname = attr.getname(); string attrvalue = attr.getvalue(); system.out.print(" " + attrname + "=\"" + attrvalue + "\""); } } system.out.print(">"); for(int i = 0; i < children.getlength(); i++) { node node = children.item(i); //获得结点的类型 short nodetype = node.getnodetype(); if(nodetype == node.element_node) { //是元素,继续递归 parseelement((element)node); } else if(nodetype == node.text_node) { //递归出口 system.out.print(node.getnodevalue()); } else if(nodetype == node.comment_node) { system.out.print("<!--"); comment comment = (comment)node; //注释内容 string data = comment.getdata(); system.out.print(data); system.out.print("-->"); } } system.out.print("</" + tagname + ">"); } }
sax:sax的全称是simple apis for xml,也即xml简单应用程序接口。与dom不同,sax提供的访问模式是一种顺序模式,这是一种快速读写xml数据的方式。当使用sax分析器对xml文档进行分析时,会触发一系列事件,并激活相应的事件处理函数,应用程序通过这些事件处理函数实现对xml文档的访问,因而sax接口也被称作事件驱动接口。
import java.io.file; import javax.xml.parsers.saxparser; import javax.xml.parsers.saxparserfactory; import org.xml.sax.attributes; import org.xml.sax.saxexception; import org.xml.sax.helpers.defaulthandler; public class saxtest1 { public static void main(string[] args) throws exception { //step1: 获得sax解析器工厂实例 saxparserfactory factory = saxparserfactory.newinstance(); //step2: 获得sax解析器实例 saxparser parser = factory.newsaxparser(); //step3: 开始进行解析 parser.parse(new file("student.xml"), new myhandler()); } } class myhandler extends defaulthandler { @override public void startdocument() throws saxexception { system.out.println("parse began"); } @override public void enddocument() throws saxexception { system.out.println("parse finished"); } @override public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception { system.out.println("start element"); } @override public void endelement(string uri, string localname, string qname) throws saxexception { system.out.println("finish element"); } }
import java.io.file; import java.util.stack; import javax.xml.parsers.saxparser; import javax.xml.parsers.saxparserfactory; import org.xml.sax.attributes; import org.xml.sax.saxexception; import org.xml.sax.helpers.defaulthandler; public class saxtest2 { public static void main(string[] args) throws exception { saxparserfactory factory = saxparserfactory.newinstance(); saxparser parser = factory.newsaxparser(); parser.parse(new file("student.xml"), new myhandler2()); } } class myhandler2 extends defaulthandler { private stack<string> stack = new stack<string>(); private string name; private string gender; private string age; @override public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception { stack.push(qname); for(int i = 0; i < attributes.getlength(); i++) { string attrname = attributes.getqname(i); string attrvalue = attributes.getvalue(i); system.out.println(attrname + "=" + attrvalue); } } @override public void characters(char[] ch, int start, int length) throws saxexception { string tag = stack.peek(); if("姓名".equals(tag)) { name = new string(ch, start,length); } else if("性别".equals(tag)) { gender = new string(ch, start, length); } else if("年龄".equals(tag)) { age = new string(ch, start, length); } } @override public void endelement(string uri, string localname, string qname) throws saxexception { stack.pop(); //表示该元素已经解析完毕,需要从栈中弹出 if("学生".equals(qname)) { system.out.println("姓名:" + name); system.out.println("性别:" + gender); system.out.println("年龄:" + age); system.out.println(); } } }
jdom:
jdom是一个开源项目,它基于树型结构,利用纯java的技术对xml文档实现解析、生成、序列化以及多种操作。()
•jdom 直接为java编程服务。它利用更为强有力的java语言的诸多特性(方法重载、集合概念等),把sax和dom的功能有效地结合起来。
•jdom是用java语言读、写、操作xml的新api函数。在直接、简单和高效的前提下,这些api函数被最大限度的优化。
jdom创建xml
import java.io.filewriter; import org.jdom.attribute; import org.jdom.comment; import org.jdom.document; import org.jdom.element; import org.jdom.output.format; import org.jdom.output.xmloutputter; public class jdomtest1 { public static void main(string[] args) throws exception { document document = new document(); element root = new element("root"); document.addcontent(root); comment comment = new comment("this is my comments"); root.addcontent(comment); element e = new element("hello"); e.setattribute("sohu", "www.sohu.com"); root.addcontent(e); element e2 = new element("world"); attribute attr = new attribute("test", "hehe"); e2.setattribute(attr); e.addcontent(e2); e2.addcontent(new element("aaa").setattribute("a", "b") .setattribute("x", "y").setattribute("gg", "hh").settext("text content")); format format = format.getprettyformat(); format.setindent(" "); // format.setencoding("gbk"); xmloutputter out = new xmloutputter(format); out.output(document, new filewriter("jdom.xml")); } }
jdom解析xml
import java.io.file; import java.io.fileoutputstream; import java.util.list; import org.jdom.attribute; import org.jdom.document; import org.jdom.element; import org.jdom.input.saxbuilder; import org.jdom.output.format; import org.jdom.output.xmloutputter; public class jdomtest2 { public static void main(string[] args) throws exception { saxbuilder builder = new saxbuilder(); document doc = builder.build(new file("jdom.xml")); element element = doc.getrootelement(); system.out.println(element.getname()); element hello = element.getchild("hello"); system.out.println(hello.gettext()); list list = hello.getattributes(); for(int i = 0 ;i < list.size(); i++) { attribute attr = (attribute)list.get(i); string attrname = attr.getname(); string attrvalue = attr.getvalue(); system.out.println(attrname + "=" + attrvalue); } hello.removechild("world"); xmloutputter out = new xmloutputter(format.getprettyformat().setindent(" ")); out.output(doc, new fileoutputstream("jdom2.xml")); } }
dom4j
import java.io.fileoutputstream; import java.io.filewriter; import org.dom4j.document; import org.dom4j.documenthelper; import org.dom4j.element; import org.dom4j.io.outputformat; import org.dom4j.io.xmlwriter; public class test1 { public static void main(string[] args) throws exception { // 创建文档并设置文档的根元素节点 :第一种方式 // document document = documenthelper.createdocument(); // // element root = documenthelper.createelement("student"); // // document.setrootelement(root); // 创建文档并设置文档的根元素节点 :第二种方式 element root = documenthelper.createelement("student"); document document = documenthelper.createdocument(root); root.addattribute("name", "zhangsan"); element helloelement = root.addelement("hello"); element worldelement = root.addelement("world"); helloelement.settext("hello"); worldelement.settext("world"); helloelement.addattribute("age", "20"); xmlwriter xmlwriter = new xmlwriter(); xmlwriter.write(document); outputformat format = new outputformat(" ", true); xmlwriter xmlwriter2 = new xmlwriter(new fileoutputstream("student2.xml"), format); xmlwriter2.write(document); xmlwriter xmlwriter3 = new xmlwriter(new filewriter("student3.xml"), format); xmlwriter3.write(document); xmlwriter3.close(); } }
import java.io.file; import java.util.iterator; import java.util.list; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import org.dom4j.document; import org.dom4j.element; import org.dom4j.io.domreader; import org.dom4j.io.saxreader; public class test2 { public static void main(string[] args) throws exception { saxreader saxreader = new saxreader(); document doc = saxreader.read(new file("student2.xml")); element root = doc.getrootelement(); system.out.println("root element: " + root.getname()); list childlist = root.elements(); system.out.println(childlist.size()); list childlist2 = root.elements("hello"); system.out.println(childlist2.size()); element first = root.element("hello"); system.out.println(first.attributevalue("age")); for(iterator iter = root.elementiterator(); iter.hasnext();) { element e = (element)iter.next(); system.out.println(e.attributevalue("age")); } system.out.println("---------------------------"); documentbuilderfactory dbf = documentbuilderfactory.newinstance(); documentbuilder db = dbf.newdocumentbuilder(); org.w3c.dom.document document = db.parse(new file("student2.xml")); domreader domreader = new domreader(); //将jaxp的document转换为dom4j的document document d = domreader.read(document); element rootelement = d.getrootelement(); system.out.println(rootelement.getname()); } }
import java.io.filewriter; import org.jdom.attribute; import org.jdom.document; import org.jdom.element; import org.jdom.output.format; import org.jdom.output.xmloutputter; public class test3 { public static void main(string[] args) throws exception { document document = new document(); element root = new element("联系人列表").setattribute(new attribute("公司", "a集团")); document.addcontent(root); element contactperson = new element("联系人"); root.addcontent(contactperson); contactperson .addcontent(new element("姓名").settext("张三")) .addcontent(new element("公司").settext("a公司")) .addcontent(new element("电话").settext("021-55556666")) .addcontent( new element("地址") .addcontent(new element("街道").settext("5街")) .addcontent(new element("城市").settext("上海")) .addcontent(new element("省份").settext("上海市"))); xmloutputter output = new xmloutputter(format.getprettyformat() .setindent(" ").setencoding("gbk")); output.output(document, new filewriter("contact.xml")); } }
下一篇: java多线程编程实现下雪效果