DOM解析XML之java

Java提供了两种XML解析器:树型解释器DOM(Document Object Model,文档对象模型),和流机制解析器SAX(Simple API for XML,XML简单API)。DOM可以将XML转换为树结构。

XML文档内容如下:

文件名:NameList.xml,文件路径:C:\Users\Tsybius\Desktop\NameList.xml

<? xml  version = "1.0"  encoding = "UTF-8" ?>
< root >
   < list1 >
     < person  id = "101"  name = "Tsybius"  remark = "1"  />
     < person  id = "102"  name = "Galatea"  remark = "2"  />
     < person  id = "103"  name = "Quintus"  remark = "3"  />
     < person  id = "104"  name = "Atia"  remark = "4"  />
     < person  id = "105"  name = "Justitia"  remark = "5"  />
   </ list1 >
   < list2 >
     < person  id = "201"  name = "Zhang"  remark = "a"  />
     < person  id = "202"  name = "Wang"  remark = "b"  />
     < person  id = "203"  name = "Li"  remark = "c"  />
     < person  id = "204"  name = "Zhao"  remark = "d"  />
     < person  id = "205"  name = "Liu"  remark = "e"  />
   </ list2 >
   < text >TEXT</ text >
</ root >
Java代码如下:
package  XmlDomTest;
 
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.NamedNodeMap;
import  org.w3c.dom.Node;
import  org.w3c.dom.NodeList;
import  org.w3c.dom.Text;
import  org.xml.sax.SAXException;
 
/**
  * 使用DOM解析XML
  * @author Tsybius
  *
  */
public  class  XmlDomTest {
     
     /**
      * 使用DOM解析XML
      * @param args
      */
     public  static  void  main(String[] args) {
 
         try  {
             
             String filePath =  "C:\\Users\\Tsybius\\Desktop\\NameList.xml" ;
             File f =  new  File(filePath);
 
             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
             DocumentBuilder builder = factory.newDocumentBuilder();
             Document doc = builder.parse(f);
             
             //遍历节点方式1
             Element root = doc.getDocumentElement();
             System.out.println( "根节点名:"  + root.getNodeName());
             NodeList children = root.getChildNodes();
             for  ( int  i =  0 ; i < children.getLength(); i++) {
                 Node child = children.item(i);
                 if  (child  instanceof  Element) {
                     Element childElement = (Element)child;
                     System.out.println( "节点名:"  + childElement.getNodeName());
                     
                     if  (childElement.getNodeName().equals( "list1" )) {
                         //list1
                         //遍历节点方式2
                         //获取指定属性
                         for  (Node list1Node = childElement.getFirstChild(); 
                                 list1Node !=  null ;
                                 list1Node = list1Node.getNextSibling()) {
                             if  (list1Node  instanceof  Element) {
                                 Element list1Element = (Element)list1Node;
                                 System.out.print( "person:" );
                                 System.out.print( "id="  + list1Element.getAttribute( "id" ) +  ";" );
                                 System.out.print( "name="  + list1Element.getAttribute( "name" ) +  ";" );
                                 System.out.print( "remark="  + list1Element.getAttribute( "remark" ) +  ";" );
                                 System.out.println();
                             }
                         }
                         
                     else  if  (childElement.getNodeName().equals( "list2" )) {
                         //list2
                         //遍历节点方式2
                         //遍历所有属性
                         for  (Node list2Node = childElement.getFirstChild(); 
                                 list2Node !=  null ;
                                 list2Node = list2Node.getNextSibling()) {
                             if  (!(list2Node  instanceof  Element)) {
                                 continue ;
                             }
                             NamedNodeMap attributes = ((Element)list2Node).getAttributes();
                             System.out.print( "person:" );
                             for  ( int  counter =  0 ; counter < attributes.getLength(); counter++) {
                                 Node attribute = attributes.item(counter);
                                 if  (attribute ==  null ) {
                                     continue ;
                                 }
                                 String name = attribute.getNodeName();
                                 String value = attribute.getNodeValue();
                                 System.out.print(name +  "="  + value +  ";" );
                             }
                             System.out.println();
                         }
                         
                     else  if  (childElement.getNodeName().equals( "text" )) {
                         //text
                         //获取文本值
                         Text textNode = (Text)childElement.getFirstChild();
                         String text = textNode.getData().trim();
                         System.out.println( "text:"  + text);
                     }
                 }
             }
 
         catch  (ParserConfigurationException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         catch  (SAXException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         catch  (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         catch  (Exception e) {
             e.printStackTrace();
         }
         
     }
}
控制台中输出结果如下:
根节点名:root
节点名:list1
person:id=101;name=Tsybius;remark=1;
person:id=102;name=Galatea;remark=2;
person:id=103;name=Quintus;remark=3;
person:id=104;name=Atia;remark=4;
person:id=105;name=Justitia;remark=5;
节点名:list2
person:id=201;name=Zhang;remark=a;
person:id=202;name=Wang;remark=b;
person:id=203;name=Li;remark=c;
person:id=204;name=Zhao;remark=d;
person:id=205;name=Liu;remark=e;
节点名:text
text:TEXT

你可能感兴趣的:(DOM解析XML之java)