java 解析 XML

本文转自:
http://hi.baidu.com/zhanghaooy/blog/item/5636af098f3ce1c43bc76371.html

在读取XML数据的时候可以采用两种实现,可以灵活应用。

XML文件为:

view plaincopy to clipboardprint?
<?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="EEWEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
<?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="EEWEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

实现一:

view plaincopy to clipboardprint?
public static void readXML(String xmlFile){

try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlFile);
Element root = doc.getDocumentElement();
NodeList ndBooks = root.getElementsByTagName("book");
NodeList ndTitles = root.getElementsByTagName("title");
NodeList ndAuthors = root.getElementsByTagName("author");
NodeList ndYears = root.getElementsByTagName("year");
NodeList ndPrices = root.getElementsByTagName("price");
if(ndBooks != null){
for(int i=0;i<ndBooks.getLength();i++){
Node ndBook = ndBooks.item(i);
Node ndTitle = ndTitles.item(i);
Node ndAuthor = ndAuthors.item(i);
Node ndYear = ndYears.item(i);
Node ndPrice = ndPrices.item(i);
System.out.println(ndBook.getAttributes().getNamedItem("category").getNodeValue());
System.out.println(ndTitle.getAttributes().getNamedItem("lang").getNodeValue());
System.out.println(ndTitle.getFirstChild().getNodeValue());
System.out.println(ndAuthor.getFirstChild().getNodeValue());
System.out.println(ndYear.getFirstChild().getNodeValue());
System.out.println(ndPrice.getFirstChild().getNodeValue());
System.out.println("--------------------------------");
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readXML(String xmlFile){

try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlFile);
Element root = doc.getDocumentElement();
NodeList ndBooks = root.getElementsByTagName("book");
NodeList ndTitles = root.getElementsByTagName("title");
NodeList ndAuthors = root.getElementsByTagName("author");
NodeList ndYears = root.getElementsByTagName("year");
NodeList ndPrices = root.getElementsByTagName("price");
if(ndBooks != null){
for(int i=0;i<ndBooks.getLength();i++){
Node ndBook = ndBooks.item(i);
Node ndTitle = ndTitles.item(i);
Node ndAuthor = ndAuthors.item(i);
Node ndYear = ndYears.item(i);
Node ndPrice = ndPrices.item(i);
System.out.println(ndBook.getAttributes().getNamedItem("category").getNodeValue());
System.out.println(ndTitle.getAttributes().getNamedItem("lang").getNodeValue());
System.out.println(ndTitle.getFirstChild().getNodeValue());
System.out.println(ndAuthor.getFirstChild().getNodeValue());
System.out.println(ndYear.getFirstChild().getNodeValue());
System.out.println(ndPrice.getFirstChild().getNodeValue());
System.out.println("--------------------------------");
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

实现二:

view plaincopy to clipboardprint?
public static void readXML2(String xmlFile){
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlFile);
Element root = doc.getDocumentElement();//root 指向bookstore节点
NodeList nls = root.getChildNodes();//nls指向bookstore的子节点book
if(nls!= null){
for(int i=0;i<nls.getLength();i++){
Node nd = nls.item(i);//输出每个book节点的category属性
if(nd.getNodeType() == Node.ELEMENT_NODE){
System.out.println("book:category:" + nd.getAttributes().getNamedItem("category").getNodeValue());
}
//以下遍历book节点的所有子节点
for(Node nde = nd.getFirstChild();nde!=null; nde = nde.getNextSibling()){
if(nde.getNodeType() == Node.ELEMENT_NODE){
//输出title节点值
if(nde.getNodeName().equals("title")){
System.out.println("----title:" + nde.getFirstChild().getNodeValue());
}
//输出author节点值
if(nde.getNodeName().equals("author")){
System.out.println("----author:" + nde.getFirstChild().getNodeValue());
}
//输出year节点值
if(nde.getNodeName().equals("year")){
System.out.println("----year:" + nde.getFirstChild().getNodeValue());
}
//输出price节点值
if(nde.getNodeName().equals("price")){
System.out.println("----price:" + nde.getFirstChild().getNodeValue());
}


}
}

}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readXML2(String xmlFile){
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlFile);
Element root = doc.getDocumentElement();//root 指向bookstore节点
NodeList nls = root.getChildNodes();//nls指向bookstore的子节点book
if(nls!= null){
for(int i=0;i<nls.getLength();i++){
Node nd = nls.item(i);//输出每个book节点的category属性
if(nd.getNodeType() == Node.ELEMENT_NODE){
System.out.println("book:category:" + nd.getAttributes().getNamedItem("category").getNodeValue());
}
//以下遍历book节点的所有子节点
for(Node nde = nd.getFirstChild();nde!=null; nde = nde.getNextSibling()){
if(nde.getNodeType() == Node.ELEMENT_NODE){
//输出title节点值
if(nde.getNodeName().equals("title")){
System.out.println("----title:" + nde.getFirstChild().getNodeValue());
}
//输出author节点值
if(nde.getNodeName().equals("author")){
System.out.println("----author:" + nde.getFirstChild().getNodeValue());
}
//输出year节点值
if(nde.getNodeName().equals("year")){
System.out.println("----year:" + nde.getFirstChild().getNodeValue());
}
//输出price节点值
if(nde.getNodeName().equals("price")){
System.out.println("----price:" + nde.getFirstChild().getNodeValue());
}


}
}

}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

第二种方法更可取些。注意理解

调用方法:

view plaincopy to clipboardprint?
public static void main(String[] args) {
String xmlFile = "E:\\workspace_g2\\TT\\src、\\books.xml";
readXML2(xmlFile);

}
public static void main(String[] args) {
String xmlFile = "E:\\workspace_g2\\TT\\src、\\books.xml";
readXML2(xmlFile);

}

相关文章参见:

http://www.blogjava.net/lcs/archive/2007/11/21/162088.html

http://achievo-bruce-126-com.iteye.com/blog/221480

http://www.91linux.com/html/article/program/java/20070901/6639.html

http://blog.csdn.net/ydsakyclguozi/archive/2008/04/30/2347575.aspx

http://blog.csdn.net/java2000_net/archive/2008/11/06/3241167.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值