百度空间 | 百度首页 
 
查看文章
 
Java解析XML文档_JDom解析XML(DOM方式)_JDom解析XML(SAX方式)
2009-08-21 18:05

XML文档解析器有很多:JAXP,JDom,Xerces,Dom4J,Xalan等。
所有解析器通常都有两种解析方式:
DOM方式解析和SAX方式解析
两种方式解析的区别:
DOM方式:
DOM方式解析就是在内存中构建整个XML文档的节点树,从而使对XML文档的操作编程对内存中节点树的操作。
该方式优点:可以方便定位节点。
缺点:需要把整个文档读入内存,然后才能构建起该文档的节点树,如果XML文档比较大时会增加系统开销。

SAX方式:
SAX方式是按照输入流的方式,按照输入的顺序解析XML文档,这种解析方式是基于时间的,文档节点的开始和结束都可以触发事件,这种解析方式是读入一部分

文档的同时就开始解析,从而减小了系统的开销,但是定位节点比较困难。

程序示例:最新的JDOM包下载地址http://jdom.org/dist/binary/ ,目前是jdom-1.1.1.zip,其下载地址:http://jdom.org/dist/binary/jdom-1.1.1.zip

Java语言: JDom解析XML(DOM方式)

package common;

import org.jdom.input.*;
import org.jdom.Attribute;
import org.jdom.Element;
import org.xml.sax.SAXException;

import java.io.*;
import java.util.List;

import org.apache.xerces.parsers.*;

public class JDomDOM {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DOMBuilder builder = new DOMBuilder();
        DOMParser parser = new DOMParser();
        String path = "D:\\student.xml";
        String output = "";
        try {
            parser.parse(path);
            org.w3c.dom.Document domDocument = parser.getDocument();
            org.jdom.Document jdomDocument = builder.build(domDocument);
            Element root = jdomDocument.getRootElement();
            output += "This XML document's root node is: " + root.getName() + "\r\n";
            List <Element> children = root.getChildren();
            output += "The root has " + children.size() + " subNodes \r\n";
            for(int i = 0; i < children.size(); i++){
                Element node = children.get(i);
                output += "In the " + i + 1 + node.getName() + "subNode: \r\n";
                List <Attribute> attrs = node.getAttributes();
                for(int k = 0; k < attrs.size(); k++){
                    Attribute attr = attrs.get(k);
                    output += "The " + k + 1 + "attribute is " + attr.getName();
                    output += " the value is: " + attr.getValue() + "\r\n";
                }
                List <Element> childrenList = node.getChildren();
                for(int j = 0; j < childrenList.size(); j++){
                    Element childNode = childrenList.get(j);
                    output += "The " + j + 1 + "subNode is: " + childNode.getName();
                    output += " value is " + childNode.getValue() + "\r\n";
                }
            }
            System.out.println(output);
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Java语言: JDom解析XML(SAX方式)

package common;

import org.jdom.input.*;
import org.jdom.*;

import java.io.*;
import java.util.List;

import org.apache.xerces.parsers.*;

public class JDomSAX {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SAXBuilder builder = new SAXBuilder();
        String path = "D:\\student.xml";
        String output = "";
        Document doc;
        try {
            doc = builder.build(path);
            Element root = doc.getRootElement();
           
            List <Element>children = root.getChildren();
            output += "sno\t" + "name\t" + "age\t" + "course\t" + "\r\n";
            for(int i = 0; i < children.size(); i++){
                Element node = children.get(i);
                Attribute attr = node.getAttribute("sno");
                output += attr.getValue() + "\t";
                output += node.getChildText("name") + "\t";
                output += node.getChildText("age") + "\t";
                output += node.getChildText("course") + "\r\n";
            }
            System.out.println(output);
        } catch (JDOMException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }
}

---------------------------------------student.xml--------------------------------

<?xml version="1.0" encoding="gb2312"?>
<students>
    <student sno="110">
        <name>Mark</name>
        <age>23</age>
        <course>English</course>
    </student>
    <student sno="114">
        <name>Andy</name>
        <age>19</age>
        <course>Chinese</course>
    </student>
</students>


类别:经验总结 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu