xPath对xml文档的处理入门5

本文介绍如何使用DOM4J库中的XPath轴选择器来定位XML文档中的元素和属性。通过实例演示了如何选取单个节点、获取所有子节点及属性等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我测试的环境是: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());
                    }
          }
}

 

基于Spring Boot搭建的一个多功能在线学习系统的实现细节。系统分为管理员用户两个主要模块。管理员负责视频、文件文章资料的管理以及系统运营维护;用户则可以进行视频播放、资料下载、参与学习论坛并享受个性化学习服务。文中重点探讨了文件下载的安全性性能优化(如使用Resource对象避免内存溢出),积分排行榜的高效实现(采用Redis Sorted Set结构),敏感词过滤机制(利用DFA算法构建内存过滤树)以及视频播放的浏览器兼容性解决方案(通过FFmpeg调整MOOV原子位置)。此外,还提到了权限管理方面自定义动态加载器的应用,提高了系统的灵活性易用性。 适合人群:对Spring Boot有一定了解,希望深入理解其实际应用的技术人员,尤其是从事在线教育平台开发的相关从业者。 使用场景及目标:适用于需要快速搭建稳定高效的在线学习平台的企业或团队。目标在于提供一套完整的解决方案,涵盖从资源管理到用户体验优化等多个方面,帮助开发者更好地理解掌握Spring Boot框架的实际运用技巧。 其他说明:文中不仅提供了具体的代码示例技术思路,还分享了许多实践经验教训,对于提高项目质量有着重要的指导意义。同时强调了安全性、性能优化等方面的重要性,确保系统能够应对大规模用户的并发访问需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值