zoukankan      html  css  js  c++  java
  • Xpath 读取XML节点

    Xpath使用:

    示例1:锁定到目标节点会使path比较长,那么可以省略一些节点:

    XPathReader reader = XPathReader.instaceFromString(xmlString);
    List<Node> nodes = reader.nodeList(
    "/Envelope/Body/InfoResponse//Test/State//DID[@type='xxx']/Software");

     

    示例2:直接到目标节点,中间可以加些过滤条件,比如索引,@attribute等(很简单 无需多言)

    XPathReader reader = XPathReader.instaceFromString(xmlString);
    List<Node> nodes = reader.nodeList("/Envelope/Body/InfoResponse/Info/CurrentCategoryList[1]/Node[@specification='XXX']/Test/State/Gateway/Category[@type='XXX']/Message[@type='XXX']/Desc");
    if (CollectionUtils.isNotEmpty(nodes)) {
    for (Node s : nodes) {
    Node sate = reader.singleNode("State[last()]", s);
    String test = sate.getAttributes().getNamedItem("state").getNodeValue();
    }
    }
     reader.close();
    }
    

     

     

     

  • 相关阅读:
    HDU
    POJ
    快速幂运算
    RocketMQ集群
    RocketMQ角色介绍
    RocketMQ初探
    MySQL 串行隔离级别小记
    分布式事务笔记
    MySQL分库分表篇
    MySQL主从篇
  • 原文地址:https://www.cnblogs.com/cnsec/p/13407143.html
Copyright © 2011-2022 走看看