简体   繁体   English

从常规xpath获取精确的xpath

[英]Get Precise xpath from general xpath

I need to do 2 thing. 我需要做两件事。

  1. Verify a value of a node in a XML file. 验证XML文件中节点的值。
  2. If the value correspond, I need to change it. 如果值对应,我需要更改它。

    I already have a complex hierarchy of object that do this job. 我已经有完成此工作的对象的复杂层次结构。 Now I'm facing a problematic case. 现在,我面临一个有问题的案件。 Lets look at an example: 让我们看一个例子:

     <staticTag> <randomTag> <anotherRandomTag> <staticTag property="id"> <staticTag> VALUE </staticTag> </staticTag> </anotherRandomTag> </randomTag> </staticTag> 

This happen n time in each XMLFile I have to parse. 这在我必须解析的每个XMLFile中发生了n次。 It would be easy with 这很容易

//staticTag[@property='id']/staticTag/text()

Yes it will return to me all the value i need to verify. 是的,它将返回我需要验证的所有值。 The problem is I don't have the precise xpath to change the value if it correspond to the value I am looking for. 问题是,如果它与我要查找的值相对应,我没有精确的xpath来更改该值。

I may post sample of code if necessary. 如果需要,我可以发布代码示例。 So tl;dr : Is there a way to generate precise xpath from a general one? 所以tl; dr:有没有一种方法可以从通用模型生成精确的xpath? As usual thank you for your time. 和往常一样,谢谢您的时间。

I have done a few more testHere is a dummy xml 我还做了一些测试这里是一个虚拟xml

<tag>
   <ZDSJG>
      <SJROT>X66P1</SJROT>
   </ZDSJG>
   <DNLVZ>
      <SJROT>VV1EZ</SJROT>
   </DNLVZ>
</tag>

My Xpath xpression that I am looking for is //SJROT With a function i wrote I can generate this XPATH from the first node : /#document/tag/ZDSJG/SJROT/#text 我正在寻找的Xpath //SJROT//SJROT使用编写的函数,我可以从第一个节点生成此XPATH: /#document/tag/ZDSJG/SJROT/#text

Unfortunately the xpath expression //SJROT don't return me the second node that have this name. 不幸的是,xpath表达式//SJROT不会向我返回具有该名称的第二个节点。 Here a snippet of code that look for the Xpath expression provided 这是寻找提供的Xpath表达式的代码片段

    XPathExpression expression = xpath.compile(expressionXPath);

    NodeList result = (NodeList) expression.evaluate(document, XPathConstants.NODE);

    List<String> generatedXpath = Lists.newArrayList();
    for (int i = 0; i < result.getLength(); i++) {
        generatedXpath.add(getXPath(result.item(i)));
    }
    return generatedXpath;

Another edit: I fixed my code, I had to use NODESET instead of NODE Will no try to generate the proper Xpath 另一个编辑:我修复了代码,我不得不使用NODESET而不是NODE不会尝试生成正确的Xpath

Since I have not found anything that would generate me the XPath from a node. 由于我还没有找到任何可以从节点生成XPath的东西。 I did my own. 我做了我自己的。 It is pretty basic and could be improve in so many ways. 这是非常基本的,可以在很多方面进行改进。 But at the moment it fit my needs. 但目前它符合我的需求。 If anybody have a better solution I will gladly accept it. 如果有人有更好的解决方案,我将很乐意接受。 Here is my java code: feel free to improve it 这是我的Java代码:随时进行改进

    public List<String> generateAbsoluteXpath(File fileToRead, String expressionXPath) throws Exception {
    DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
    Document document = documentFactory.newDocumentBuilder().parse(fileToRead);

    XPathFactory xpathFactory = XPathFactory.newInstance();
    XPath xpath = xpathFactory.newXPath();

    XPathExpression expression = xpath.compile(expressionXPath);
    NodeList result = (NodeList) expression.evaluate(document, XPathConstants.NODESET);

    List<String> generatedXpath = Lists.newArrayList();
    for (int i = 0; i < result.getLength(); i++) {
        generatedXpath.add(getXPath(result.item(i)));
    }
    return generatedXpath;
}



public String getXPath(Node node)
{
    Node parent = node.getParentNode();
    if (parent == null || parent.getNodeName().equals("#document")) {
        return "/" + node.getNodeName();
    }
    return getXPath(parent) + "/" + node.getNodeName();
}

So Basically, I input an XPath that will generate multiple node. 因此,基本上,我输入了将生成多个节点的XPath。 It return me the list of absolute xpath referencing the node that were found with the general path. 它返回给我的绝对xpath列表,该列表引用了用常规路径找到的节点。 Since i have the absolute xpath the other object I am working with may now read / replace the value. 由于我具有绝对的xpath,因此我正在使用的其他对象现在可以读取/替换该值。 Problem solved. 问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM