数据加载中……
JAVA XML 解析之二 -DOM
2008年4月21日  Edited By DingDangXiaoMa
DOM 方法对XML的解析,是读取整个.xml文件,把信息存储到内存中形成树型结构,然后再对各结点进行处理。
好处:简单,方便。
坏处:由于读取整个文件再进行处理,处理大文件时,占用大量内存资料。
example:
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;
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;
public class MyDOMParser {
    
// 名字空间
    private String strNamespace = "http://www.lit.edu.cn/student/";
    
// 一个学生的资料
    private Hashtable htbStudent = new Hashtable();
    
// 所有学生的向量列表
    private Vector vStuInfo = new Vector();
    
public MyDOMParser() {
    }
    
public static void main(String[] args) {
        MyDOMParser myDOMParser 
= new MyDOMParser();
        myDOMParser.parseXMLFile(
"http://localhost/example/xml/SutInfo.xml");
    }
    
/**
     * 解析文档
     * 
@param fileURI
     
*/
    
public void parseXMLFile(String fileURI) {
        
try {
            DocumentBuilderFactory factory 
= DocumentBuilderFactory.newInstance();
            
// 允许名字空间
            factory.setNamespaceAware(true);
            
// 允许验证
            factory.setValidating(true);
            
// 获得DocumentBuilder的一个实例
            DocumentBuilder builder = factory.newDocumentBuilder();
            
// 解析文档,并获得一个Document实例。
            Document doc = builder.parse(fileURI);
            
// 获得根节点StuInfo
            Element elmtStuInfo = doc.getDocumentElement();
            
// 得到所有student节点
            NodeList nlStudent = elmtStuInfo.getElementsByTagNameNS(strNamespace, "student");
            System.out.println(
"**** Student information start ****");
            
// 循环输出每一个学生资料
            for (int i = 0; i < nlStudent.getLength(); i++) {
                
// 当前student节点元素
                Element elmtStudent = (Element) nlStudent.item(i);
                NodeList nlCurrent 
= elmtStudent.getElementsByTagNameNS(strNamespace, "name");
                System.out.println(
"Name:"+ nlCurrent.item(0).getFirstChild().getNodeValue());
                nlCurrent 
= elmtStudent.getElementsByTagNameNS(strNamespace,"sex");
                System.out.println(
"Sex:"+ nlCurrent.item(0).getFirstChild().getNodeValue());
                nlCurrent 
= elmtStudent.getElementsByTagNameNS(strNamespace,"lesson");
                
for (int j = 0; j < nlCurrent.getLength(); j++) {
                    Element elmtLesson 
= (Element) nlCurrent.item(j);
                    NodeList nlLesson 
= elmtLesson.getElementsByTagNameNS(strNamespace, "lessonName");
                    System.out.print(nlLesson.item(
0).getFirstChild().getNodeValue());
                    System.out.print(
":");
                    nlLesson 
= elmtLesson.getElementsByTagNameNS(strNamespace,"lessonScore");
                    System.out.print(nlLesson.item(
0).getFirstChild().getNodeValue());
                    System.out.print(
"\n");
                }
                System.out.println(
"------------------------------------");
            }
            System.out.println(
"**** Student information end ****");
        } 
catch (SAXException saxe) {
            System.out.println(
"error1");
            System.out.println(saxe.getMessage());
        } 
catch (IOException ioe) {
            System.out.println(
"error2");
            System.out.println(ioe.getMessage());
        } 
catch (ParserConfigurationException pce) {
            System.out.println(
"error3");
            System.out.println(pce.getMessage());
        }
    }
}
输出结果如下:
**** Student information start ****
Name:bigmouse
Sex:male
math:
60
Englist:
59
autoCAD:
80
SCM:
90
mechanics:
61
------------------------------------
Name:coco
Sex:female
math:
90
Englist:
95
C
++:80
Java:
85
------------------------------------
**** Student information end ****
分析:
1.从上面导入的包就可以看出主要应用:javax.xml.parsers 和 org.w3c.dom  这两个包都是基础包,jdk 中有。因此DOM方式不用导入其它包就可运行。又可以看出得到了w3c组织的认可。其实这个DOM 方式还结合了.jaxp 也就是java api for xml parser
2.最主要的一点,是命名空间的使用,所有的结点也都在在命名空间中所定义的。
3。读取xml产生Document 对象。NodeList 中遍历所的结点。
这个例子就到这里,下一篇是JDOM方法。

posted on 2008-04-21 16:54 叮当小马 阅读(282) 评论(0)  编辑  收藏 所属分类: XML


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


网站导航: