xPath对xml文档的处理入门4

我测试的环境是:WindowsXP、eclipse3.2、jdk1.6、dom4j1.6.1.jar、z-dom4j-1.4.jar
其中dom4j1.6.1.jar、z-dom4j-1.4.jar都可以在本人的资源库中进行下载,还有Dom4j的API,以及学习文档。
xml文档是:
<?xml version="1.0" encoding="GBK"?>
<root name="根节点">
 <book name="书" address="北京">
  <titlechild name="1" address="北京" author="马士兵">CS从入门到精通</titlechild>
  <titlechild1 name="2" address="天津" author="a马士兵" price="37">aCS从入门到精通titlechild1</titlechild1>
  <titlechild2 name="3" address="南京" author="a马士兵" price="32">aCS从入门到精通titlechild2</titlechild2>
  <titlechild name="4" address="上海" author="title2马士兵" price="55">title2CS从入门到精通</titlechild>
  <titlechild name="5" address="西安" author="title3马士兵" price="34">title3CS从入门到精通</titlechild>
 </book>
 <title name="6" address="深圳" author="深圳1" price="34">深圳</title>
</root>
测试代码是:
package org.dom4j.document_study_XPath;

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

import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class Test_XPath {

         public static void main(String[] args) {
                  readexml("C:\\2.xml");
         }
         //解析原始的xml
         public static void readexml(String xmlpath){
                 SAXReader readxml = new SAXReader();
                 Document doc = null;  
                 try {
                        doc = readxml.read(new File(xmlpath));//解析原始的xml
                        TestSelectNodes_06(doc);
                        TestSelectNodes_07(doc);
                 } catch (Exception e) {
                        e.printStackTrace();
                 }
         }
         //通过节点的XPath路径(通配符*)获取document某个名称,属性的节点 document 一个Dom4j的Document对象
        @SuppressWarnings("unchecked")
        public static void TestSelectNodes_06(Document document) {
                System.out.println("获取名称为book节点下的所有子节点:");
                List<Node> nodelist =document.selectNodes("//book/*");
                for(Node node1_2 : nodelist){
                         System.out.println("node节点名称name=" +node1_2.getName() + "\t节点属性name=" + node1_2.valueOf("@name"));
                }
                System.out.println("获取xml文档的所有节点");
                List<Node> nodelist1 =document.selectNodes("//*");
                for(Node node1_2 : nodelist1){
                        System.out.println("node节点名称name=" +node1_2.getName() + "\t节点属性name=" + node1_2.valueOf("@name"));
                 }
                System.out.println("获取名称为pricechild的节点的所有属性");
                 List<Node> nodelist3 =document.selectNodes("//title[@*]");
                 for(Node node1_2 : nodelist3){
                        System.out.println("node节点名称name=" +node1_2.getName() + "\t节点属性name=" + node1_2.valueOf("@name"));
                }
        }
        //通过节点的XPath路径(运算符|)获取document某个名称,属性的节点 document 一个Dom4j的Document对象
       @SuppressWarnings("unchecked")
       public static void TestSelectNodes_07(Document document) {
               System.out.println("选取xml文件中所有book下的节点titlechild1和节点的titlechild2");
               List<Node> nodelist =document.selectNodes("//book/titlechild2|//book/titlechild1");
               for(Node node1_2 : nodelist){
                       System.out.println("node节点名称name=" +node1_2.getName() + "\t节点属性name=" + node1_2.valueOf("@name"));
               }
               System.out.println("获取xml文件中所有的titlechild1和titlechild2节点");
               List<Node> nodelist1 =document.selectNodes("//titlechild2|//titlechild1");
               for(Node node1_2 : nodelist1){
                       System.out.println("node节点名称name=" +node1_2.getName() + "\t节点属性name=" + node1_2.valueOf("@name"));
               }
               System.out.println("获取xml文件中所有titlechild1节点和root/book/title下的所有titlechild节点");
                List<Node> nodelist3 =document.selectNodes("/root/book//titlechild|//titlechild1");
                for(Node node1_2 : nodelist3){
                         System.out.println("node节点名称name=" +node1_2.getName() + "\t节点属性name=" + node1_2.valueOf("@name"));
                }
        }
}

 

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