java - 如何在java中获取XML文件中节点的完整路径?

标签 java xml xpath nodelist xmlnodelist

我有一个由 XPath 解析的 XML 文件以获取其中的所有参数,然后我想获取每个参数的完整路径,但我的代码由于某种原因无法正常工作。 代码:

ArrayList<String> names = new ArrayList<String>();
ArrayList<String> paths = new ArrayList<String>();
URL oracle = new URL("http://weather.yahooapis.com/forecastrss?w=2502265");
InputStream is = oracle.openStream();
org.w3c.dom.Document doc = null;
DocumentBuilderFactory domFactory;
DocumentBuilder builder;
try {
    domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    builder = domFactory.newDocumentBuilder();
    doc = builder.parse(is);
} catch (Exception ex) {
    System.err.println("unable to load XML: " + ex);
}

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//*/@*");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nl = (NodeList) result;
for(int j=0 ; j < nl.getLength() ; j++){
    names.add(nl.item(j).getNodeName());
    Node node = nl.item(j);
    ArrayList<String> parents = new ArrayList<String>();
    while(node.getParentNode() != null){ // it didn't even gone through this loop
        parents.add(node.getNodeName());
        node = node.getParentNode();
    }
    System.out.println(parents);
}       

最佳答案

表达式 //*/@* 返回一个空的 NodeSet。

下面的代码检索您需要的路径:

import org.w3c.dom.Attr;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SO {

   @SuppressWarnings("nls")
   public static void main( String[] args ) throws Exception {
      List< String > names = new ArrayList<>();
      URL oracle =
         new URL( "http://weather.yahooapis.com/forecastrss?w=2502265" );
      InputStream is = oracle.openStream();
      org.w3c.dom.Document doc = null;
      DocumentBuilderFactory domFactory;
      DocumentBuilder builder;
      try {
         domFactory = DocumentBuilderFactory.newInstance();
         domFactory.setNamespaceAware(true);
         builder = domFactory.newDocumentBuilder();
         doc = builder.parse(is);
      } catch (Exception ex) {
         System.err.println("unable to load XML: " + ex);
      }
      XPathFactory factory = XPathFactory.newInstance();
      XPath xpath = factory.newXPath();
      XPathExpression expr = xpath.compile( "//*:*/@*" );
      Object result = expr.evaluate( doc, XPathConstants.NODESET );
      NodeList nl = (NodeList) result;
      for(int j=0 ; j < nl.getLength() ; j++){
         names.add( nl.item(j).getNodeName());
         Node node = nl.item(j);
         String path = "." + node.getNodeName() + " = " + node.getNodeValue();
         node = ((Attr)node).getOwnerElement();
         while( node  != null) {
            path = node.getNodeName() + '/' + path;
            node = node.getParentNode();
         }
         System.out.println( path );
      }
   }
}

关于java - 如何在java中获取XML文件中节点的完整路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16634267/

相关文章:

java - 在谷歌地图中为我的位置按钮设置自定义 View

xml - 需要用 xmlpath/xmlsearch 提取数据

xpath - XPath选择具有属性值和子节点值的节点

java - 安装后通过 Google Protect 阻止应用程序

java - 字符串拆分和比较 - 最快的方法

java - 通过 Java 中的 ANT 针对所有平台将 Web 项目部署到 Glassfish

java - 使用 Java split() 方法将 XML 字符串拆分为多个 XML 文档

xml - sql server 2008 xml文件到表

java - 使用 java 正则表达式重新排序名称

xml - Xform 输入字段中允许的字符限制