Dom 解析XML
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<data>
<book id="1">
<name>Android应用开发详解</name>
<author>json</author>
<price>88</price>
<pubinfo>人民邮电出版社</pubinfo>
</book>
<book id="2">
<name>Android权威指南</name>
<author>tom</author>
<price>79</price>
<pubinfo>人民教育出版社</pubinfo>
</book>
<book id="3">
<name>Android开发案例大全</name>
<author>mark</author>
<price>68</price>
<pubinfo>电子工业出版社</pubinfo>
</book>
<book id="4">
<name>Android从入门到精通</name>
<author>jack</author>
<price>68</price>
<pubinfo>电子工业出版社</pubinfo>
</book>
<book id="5">
<name>Pro Spring</name>
<author>mark</author>
<price>68</price>
<pubinfo>电子工业出版社</pubinfo>
</book>
</data>
<data>
<book id="1">
<name>Android应用开发详解</name>
<author>json</author>
<price>88</price>
<pubinfo>人民邮电出版社</pubinfo>
</book>
<book id="2">
<name>Android权威指南</name>
<author>tom</author>
<price>79</price>
<pubinfo>人民教育出版社</pubinfo>
</book>
<book id="3">
<name>Android开发案例大全</name>
<author>mark</author>
<price>68</price>
<pubinfo>电子工业出版社</pubinfo>
</book>
<book id="4">
<name>Android从入门到精通</name>
<author>jack</author>
<price>68</price>
<pubinfo>电子工业出版社</pubinfo>
</book>
<book id="5">
<name>Pro Spring</name>
<author>mark</author>
<price>68</price>
<pubinfo>电子工业出版社</pubinfo>
</book>
</data>
解析类
package com.dom;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* <pre>
* dom解析xml
* <pre>
* @author scott
*
*/
public class DomXmlParser {
public static void main(String[] args) {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
File file=new File("D:\\workspace\\demo\\src\\books.xml");
DocumentBuilder documentBuilder=null;
try {
documentBuilder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document document=null;
try {
document=documentBuilder.parse(file);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element element=document.getDocumentElement();
NodeList nodeList=element.getElementsByTagName("book");
for (int i = 0; i < nodeList.getLength(); i++) {
Element book = (Element)nodeList.item(i);
String id=book.getAttribute("id");
Element bookname=(Element) book.getElementsByTagName("name").item(0);
String name=bookname.getFirstChild().getNodeValue();
Element bookauthor=(Element) book.getElementsByTagName("author").item(0);
String author=bookauthor.getFirstChild().getNodeValue();
Element bookprice=(Element) book.getElementsByTagName("price").item(0);
String price=bookprice.getFirstChild().getNodeValue();
Element bookpubinfo=(Element) book.getElementsByTagName("pubinfo").item(0);
String pubinfo=bookpubinfo.getFirstChild().getNodeValue();
System.out.println(id+","+name+","+author+","+price+","+pubinfo);
}
}
}
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* <pre>
* dom解析xml
* <pre>
* @author scott
*
*/
public class DomXmlParser {
public static void main(String[] args) {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
File file=new File("D:\\workspace\\demo\\src\\books.xml");
DocumentBuilder documentBuilder=null;
try {
documentBuilder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document document=null;
try {
document=documentBuilder.parse(file);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element element=document.getDocumentElement();
NodeList nodeList=element.getElementsByTagName("book");
for (int i = 0; i < nodeList.getLength(); i++) {
Element book = (Element)nodeList.item(i);
String id=book.getAttribute("id");
Element bookname=(Element) book.getElementsByTagName("name").item(0);
String name=bookname.getFirstChild().getNodeValue();
Element bookauthor=(Element) book.getElementsByTagName("author").item(0);
String author=bookauthor.getFirstChild().getNodeValue();
Element bookprice=(Element) book.getElementsByTagName("price").item(0);
String price=bookprice.getFirstChild().getNodeValue();
Element bookpubinfo=(Element) book.getElementsByTagName("pubinfo").item(0);
String pubinfo=bookpubinfo.getFirstChild().getNodeValue();
System.out.println(id+","+name+","+author+","+price+","+pubinfo);
}
}
}
效果
1,Android应用开发详解,json,88,人民邮电出版社
2,Android权威指南,tom,79,人民教育出版社
3,Android开发案例大全,mark,68,电子工业出版社
4,Android从入门到精通,jack,68,电子工业出版社
5,Pro Spring,mark,68,电子工业出版社