有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

共 (1) 个答案

  1. # 1 楼答案

    表达式//*/@*返回一个空节点集

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

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