在上一篇随笔中分析了xml以及它的两种验证方式。我们有了xml,但是里面的内容要怎么才能得到呢?如果得不到的话,那么还是没用的,解析xml的方式主要有DOMSAX,其中DOM是W3C官方的解析方式,而SAX是民间(非官方)的,两种解析方式是很不一样的。下面通过例子来分析两种解析方式的区别。

下面是要解析的xml文档

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <学生名册>
 3 <!--http://www.cnblogs.com/zhi-hao/-->
 4     <学生 学号="A1">
 5         <姓名>CIACs</姓名>
 6         <性别></性别>
 7         <年龄>22</年龄>
 8     </学生>
 9     <学生 学号="A2">
10         <姓名>zhihao</姓名>
11         <性别></性别>
12         <年龄>23</年龄>
13     </学生>
14 </学生名册>

 

DOM(Document Object Model)文档对象模式,从名字上就可以知道,DOM应该是基于文档对象来解析的。在DOM解析方式中主要用到了以下四个接口

1、Document接口,该接口是对xml文档进行操作的入口,要想操作xml,必须获得文档的入口。

2、Node接口,存储xml文档的节点的

3、NodeList接口

4、NameNodeMap接口,存储的是xml中的属性。

DOM中的基本对象有Document,Node,NodeList,Element和Attr。有了这些就可以解析xml了

 

 1 package xmlTest;
 2 
 3 import java.io.File;
 4 
 5 import javax.xml.parsers.DocumentBuilder;
 6 import javax.xml.parsers.DocumentBuilderFactory;
 7 
 8 import org.w3c.dom.Attr;
 9 import org.w3c.dom.Document;
10 import org.w3c.dom.Element;
11 import org.w3c.dom.NamedNodeMap;
12 import org.w3c.dom.Node;
13 import org.w3c.dom.NodeList;
14 /**
15  * 
16  * @author CIACs
17  * 2014-09-22
18  */
19 
20 public class DOM {
21 
22     public static void main(String[] args) throws Exception {
23         //获得解析工厂实例
24         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
25         //通过工厂获得DocumentBulider
26         DocumentBuilder db = dbf.newDocumentBuilder();
27         //获得文档对象的入口
28         Document doc = db.parse(new File("student.xml"));
29         //获得根元素
30         Element root = doc.getDocumentElement();
31         //开始解析
32         parseElement(root);
33     }
34     private static void parseElement(Element element)
35     {
36         String tagName = element.getNodeName();
37         System.out.print("<"+tagName);
38         //获得元素属性
39         NamedNodeMap map = element.getAttributes();
40         if(null != map)
41         {
42             for(int i = 0;i < map.getLength();i++)
43             {
44                 Attr attr = (Attr)map.item(i);
45                 String attrName = attr.getName();
46                 String attrValue = attr.getValue();
47                 System.out.print(" "+attrName + "=\""+attrValue+"\"");
48             }
49         }
50         System.out.print(">");
51         
52         //获得元素的孩子节点
53         NodeList child = element.getChildNodes();
54         
55         for(int i = 0;i < child.getLength();i++)
56         {
57             Node node = child.item(i);
58             //判断节点类型
59             
60             short nodeType = node.getNodeType();
61         
62             if(nodeType == Node.ELEMENT_NODE)
63             {
64                 parseElement((Element)node);
65             }
66             else 
67                 if(nodeType == Node.TEXT_NODE)
68                 {
69                     System.out.print(node.getTextContent());
70                 }
71                 else
72                     if(nodeType == Node.COMMENT_NODE)
73                     {
74                         System.out.print("<!--"+node.getTextContent()+"-->");
75                     }
76         }
77         System.out.print("</"+tagName+">");
78     }
79 }
DOM

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-09
  • 2022-01-19
  • 2021-11-15
  • 2022-12-23
  • 2021-05-22
  • 2022-02-20
猜你喜欢
  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-16
  • 2022-01-02
  • 2022-12-23
  • 2022-02-21
相关资源
相似解决方案