xPath对xml文档的处理入门5

我测试的环境是:WindowsXP、eclipse3.2、jdk1.6、dom4j1.6.1.jar、z-dom4j-1.4.jar

本节介绍:xpath中的轴:

xml文件为:

<?xml version="1.0" encoding="GBK"?>
<root name="根节点">
 <book name="书" address="北京">
  <title name="标题">
   <titlechild name="书名:CS从入门到精通" address = '北京上海南京' lang ='23'>CS从入门到精通</titlechild>
   <titlechild1 name="titlechild1入门到精通">CS从入门到精通</titlechild1>
   <titlechild2 name="titlechild2入门到精通">CS从入门到精通</titlechild2>
  </title>
  <title name="标题">
   <titlechild name="title2书名:CS从入门到精通">
    <titlechild name="title3书名:CS从入门到精通">title3CS从入门到精通</titlechild>
   </titlechild>
  </title>
  <author name="作者">
   <authorchild name="书名:java从入门到精通">候捷</authorchild>
  </author>
  <price name="价格">
   <pricechild name="书名:oracle数据库入门">58.3</pricechild>
  </price>
 </book>
 <price name="价格" address ="陕西">
   <pricechild name="数据库入门">600</pricechild>
 </price>
</root>

测试代码是:

package org.dom4j.document_study_XPath;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XPathTest_02 {
         public static void main(String[] args) {
                  readexml("C:\\seven.xml");
         }
         //解析原始的xml
         public static void readexml(String xmlpath){
                 SAXReader readxml = new SAXReader();
                 Document doc = null;  
                 try {
                          doc = readxml.read(new File(xmlpath));//解析原始的xml
                          TestSelectSingleNode(doc);
                          TestSelectSingleNode_01(doc);
                          TestSelectNodes_01(doc);
                 } catch (Exception e) {
                          e.printStackTrace();
                 }
         }
         //通过节点的XPath轴获取一个节点 document 一个Dom4j的Document对象
         public static void TestSelectSingleNode(Document document) {
                  Node node  = document.selectSingleNode("child::root");//获取当前节点,child是固定的,root是xml的根节点
                  Node node1 = document.selectSingleNode("child::root/book");//获取根节点下的book节点
                  System.out.println(node.getName());
                  System.out.println(node1.getName());
   
         }
         //通过节点的XPath轴获取一个节点 document 一个Dom4j的Document对象
        @SuppressWarnings("unchecked")
        public static void TestSelectSingleNode_01(Document document) {
                 //获取当前节点(此时是xml)的所有子节点,所以此时的子节点指的是根节点
                 List<Node> listnode0  = document.selectNodes("child::/*");
                 //List<Node> listnode0  = document.selectNodes("child::node()");
                 for(Node node : listnode0){
                          System.out.println("xml文件的根节点是:"+node.getName());
                 }
                 List<Node> listnode  = document.selectNodes("child:://*");
                 for(Node node : listnode){
                          System.out.println("xml文件的所有节点是:"+node.getName());
                 }
                 List<Node> listnode1  = document.selectNodes("child::/root/*");
                 for(Node node : listnode1){
                        System.out.println("根节点的直接子节点是:"+node.getName());
                 }
                 List<Node> listnode2  = document.selectNodes("child::/root//*");
                 for(Node node : listnode2){
                        System.out.println("根节点的所有子节点是:"+node.getName());
                 }
         }
 
         //通过节点的XPath轴获取一个节点 document 一个Dom4j的Document对象
        @SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
         public static void TestSelectNodes_01(Document document) {
                   Node node =document.selectSingleNode("//child::/root/book/title/titlechild");
                   System.out.println("节点的名称是:"+node.getName() + "address属性如下:");
                    List<Attribute> listattribute = node.selectNodes("attribute::address");
                    for(Attribute a : listattribute){
                              System.out.println("属性name=" +a.getName() + "\t\t value=" + a.getText());
                    }
                    System.out.println("======================================");
                    Node node1 =document.selectSingleNode("//child::/root/book/title/titlechild");
                    System.out.println("节点的名称是:"+node.getName() + "的所有属性如下:");
                    List<Attribute> listattribute1 = node1.selectNodes("attribute::*");
                    for(Attribute a : listattribute1){
                             System.out.println("属性name=" +a.getName() + "\t\t value=" + a.getText());
                    }
          }
}

 

你可能感兴趣的:(xPath对xml文档的处理入门5)