簡體   English   中英

如何在java中獲取XML文件中節點的完整路徑?

[英]How to get the full path of a node in XML file in java?

我有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 );
      }
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM