温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
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>
解析类
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);
            
        }
        
        
        
    }

}
效果
1,Android应用开发详解,json,88,人民邮电出版社
2,Android权威指南,tom,79,人民教育出版社
3,Android开发案例大全,mark,68,电子工业出版社
4,Android从入门到精通,jack,68,电子工业出版社
5,Pro Spring,mark,68,电子工业出版社

posted on 2011-08-24 11:32 雪山飞鹄 阅读(415) 评论(0)  编辑  收藏 所属分类: javaseandroid

只有注册用户登录后才能发表评论。


网站导航: