java读取解析xml文件实例
程序员文章站
2024-03-02 15:24:52
读取本地的xml文件,通过dom进行解析,dom解析的特点就是把整个xml文件装载入内存中,形成一颗dom树形结构,树结构是方便遍历和和操纵。
dom解析的特性就是读取x...
读取本地的xml文件,通过dom进行解析,dom解析的特点就是把整个xml文件装载入内存中,形成一颗dom树形结构,树结构是方便遍历和和操纵。
dom解析的特性就是读取xml文件转换为 dom树形结构,通过节点进行遍历。
这是w3c关于节点的概念
如果xml中包含有大量的数据,由于dom一次性把xml装入内存中的特性,所以dom不适合于包含大量数据的xml解析。当包含有大量xml的时候,用sax进行解析比较节省内存。
下面是一个运用dom进行解析xml文件的例子:
xml文件结构如下:
<?xml version="1.0" encoding="iso-8859-1"?> <bookstore> <book category="cooking"> <title lang="en">everyday italian</title> <author>giada de laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">harry potter</title> <author>j k. rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">xquery kick start</title> <author>james mcgovern</author> <year>2003</year> <price>49.99</price> </book> <book category="web" cover="paperback"> <title lang="en">learning xml</title> <author>erik t. ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
创建解析xml的类如下:
package 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.node; import org.w3c.dom.nodelist; public class readxmlfile { public static void main(string[] args) { try{ file xmlfile = new file("src/resource/book.xml"); documentbuilderfactory builderfactory = documentbuilderfactory.newinstance(); documentbuilder builder = builderfactory.newdocumentbuilder(); document doc = builder.parse(xmlfile); doc.getdocumentelement().normalize(); system.out.println("root element: "+doc.getdocumentelement().getnodename()); nodelist nlist = doc.getelementsbytagname("book"); for(int i = 0 ; i<nlist.getlength();i++){ node node = nlist.item(i); system.out.println("node name: "+ node.getnodename()); element ele = (element)node; system.out.println("----------------------------"); if(node.getnodetype() == element.element_node){ system.out.println("book category: "+ ele.getattribute("category")); system.out.println("title name: "+ ele.getelementsbytagname("title").item(0).gettextcontent()); system.out.println("author name: "+ele.getelementsbytagname("author").item(0).gettextcontent()); system.out.println("year :"+ele.getelementsbytagname("year").item(0).gettextcontent()); system.out.println("price : "+ele.getelementsbytagname("price").item(0).gettextcontent()); system.out.println("-------------------------"); } }
解析结果:
root element: bookstore node name: book ---------------------------- book category: cooking title name: everyday italian author name: giada de laurentiis year :2005 price : 30.00 ------------------------- node name: book ---------------------------- book category: children title name: harry potter author name: j k. rowling year :2005 price : 29.99 ------------------------- node name: book ---------------------------- book category: web title name: xquery kick start author name: james mcgovern year :2003 price : 49.99 ------------------------- node name: book ---------------------------- book category: web title name: learning xml author name: erik t. ray year :2003 price : 39.95 -------------------------
以上是通过name获得对应的值,
下面利用循环节点的方式输出:
循环节点输出方式的代码如下:
package 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.namednodemap; import org.w3c.dom.node; import org.w3c.dom.nodelist; public class readxmlfile2 { public static void main(string[] args) { try{ file xmlfile = new file("src/resource/book.xml"); documentbuilderfactory builderfactory = documentbuilderfactory.newinstance(); documentbuilder builder = builderfactory.newdocumentbuilder(); document doc = builder.parse(xmlfile); doc.getdocumentelement().normalize(); system.out.println("root element: "+doc.getdocumentelement().getnodename()); if(doc.haschildnodes()){ printnode(doc.getchildnodes()); } }catch(exception e){ e.printstacktrace(); } } public static void printnode(nodelist nodelist){ system.out.println("------------------------"); // system.out.println(nodelist.getlength()); for(int i = 0; i<nodelist.getlength(); i++){ node node = (node)nodelist.item(i); if(node.getnodetype() == node.element_node){ system.out.println("node name: "+node.getnodename()); system.out.println("node value: "+node.gettextcontent()); if(node.hasattributes()){ namednodemap nodemap = node.getattributes(); for(int j = 0; j < nodemap.getlength() ; j++){ node nodenew = nodemap.item(j); system.out.println("node name "+nodenew.getnodename()); system.out.println("node value "+nodenew.getnodevalue()); } } if(node.haschildnodes()){ printnode(node.getchildnodes()); } } } } }
输出结果如下:
root element: bookstore ------------------------ node name: bookstore node value: everyday italian giada de laurentiis 2005 30.00 harry potter j k. rowling 2005 29.99 xquery kick start james mcgovern 2003 49.99 learning xml erik t. ray 2003 39.95 ------------------------ node name: book node value: everyday italian giada de laurentiis 2005 30.00 node name category node value cooking ------------------------ node name: title node value: everyday italian node name lang node value en ------------------------ node name: author node value: giada de laurentiis ------------------------ node name: year node value: 2005 ------------------------ node name: price node value: 30.00 ------------------------ node name: book node value: harry potter j k. rowling 2005 29.99 node name category node value children ------------------------ node name: title node value: harry potter node name lang node value en ------------------------ node name: author node value: j k. rowling ------------------------ node name: year node value: 2005 ------------------------ node name: price node value: 29.99 ------------------------ node name: book node value: xquery kick start james mcgovern 2003 49.99 node name category node value web ------------------------ node name: title node value: xquery kick start node name lang node value en ------------------------ node name: author node value: james mcgovern ------------------------ node name: year node value: 2003 ------------------------ node name: price node value: 49.99 ------------------------ node name: book node value: learning xml erik t. ray 2003 39.95 node name category node value web node name cover node value paperback ------------------------ node name: title node value: learning xml node name lang node value en ------------------------ node name: author node value: erik t. ray ------------------------ node name: year node value: 2003 ------------------------ node name: price node value: 39.95 ------------------------
关于节点的问题:
<book category="cooking"> <title lang="en">everyday italian</title> <author>giada de laurentiis</author> <year>2005</year> <price>30.00</price> </book>
对于 book应用:doc.getchildnodes() 得到一个nodelist其中nodelist的长度为9
9个节点分别如下:
title节点
lang节点
everyday节点
author节点
giada de laurentiis节点
year节点
2005节点
price节点
30.00节点
ps:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线xml/json互相转换工具:
在线格式化xml/在线压缩xml:
xml在线压缩/格式化工具: