Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中获取XML文件中节点的完整路径?_Java_Xml_Xpath_Nodelist_Xmlnodelist - Fatal编程技术网

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

如何在java中获取XML文件中节点的完整路径?,java,xml,xpath,nodelist,xmlnodelist,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/forecastrs

我用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);
}       
ArrayList name=new ArrayList();
ArrayList路径=新的ArrayList();
URL oracle=新URL(“http://weather.yahooapis.com/forecastrss?w=2502265");
InputStream is=oracle.openStream();
org.w3c.dom.Document doc=null;
文件建设者工厂;
文档生成器;
试一试{
domFactory=DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
builder=domFactory.newDocumentBuilder();
doc=builder.parse(is);
}捕获(例外情况除外){
System.err.println(“无法加载XML:+ex”);
}
XPathFactory=XPathFactory.newInstance();
XPath=factory.newXPath();
XPathExpression expr=xpath.compile(“/*/@*”);
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表nl=(节点列表)结果;
对于(int j=0;j
表达式
/*/@*
返回一个空节点集

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

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 );
      }
   }
}
import org.w3c.dom.Attr;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
公开课{
@抑制警告(“nls”)
公共静态void main(字符串[]args)引发异常{
Listnames=newarraylist();
URL甲骨文=
新网址(“http://weather.yahooapis.com/forecastrss?w=2502265" );
InputStream is=oracle.openStream();
org.w3c.dom.Document doc=null;
文件建设者工厂;
文档生成器;
试一试{
domFactory=DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
builder=domFactory.newDocumentBuilder();
doc=builder.parse(is);
}捕获(例外情况除外){
System.err.println(“无法加载XML:+ex”);
}
XPathFactory=XPathFactory.newInstance();
XPath=factory.newXPath();
XPathExpression expr=xpath.compile(“//*:*/@*”);
对象结果=expr.evaluate(doc,XPathConstants.NODESET);
节点列表nl=(节点列表)结果;
对于(int j=0;j
哦,对不起,我已经这样做了,但是正确的XPath表达式是/@*,请注意对Attr的转换,以及使用getOwnerElement()代替getParent()XPath表达式可以,但是对于名称空间(
xmlns:yweather=)http://xml.weather.yahoo.com/ns/rss/1.0“xmlns:geo=”http://www.w3.org/2003/01/geo/wgs84_pos#“
),要么他必须使用查询所有名称空间的
/*:*/@*
,要么用另一种方式处理。嗯,thx,我得到了路径,但格式不正确,因为当我尝试查询它时,有很多异常,我只想再次使用路径,生成的路径如(#document/rss/channel/item/yweather:forecast/.code)无法使用。我添加了节点和属性之间缺少一个
/
。但最终这两个查询是相等的,而您的查询可能是更优雅的查询。我只是想指出名称空间的问题。如果你对XPath2.0没问题,我在回答另一个问题时提出了一个表达式来构建这些路径:-它对所有atribute也很好;返回包含每个属性路径的XPath表达式列表。