Menu

Getting the reference to the Parent Node directly from the Comparison.Detail object

MonishaM
2019-06-27
2019-07-02
  • MonishaM

    MonishaM - 2019-06-27

    In our project, we are comparing two XMLs and are selectively updating the control XML based on this comparison. In certain cases the Control/Test node is null (as fetched from the Comparison.Detail object). Manipulating / updating the Control/Test XML in such a case requires us to compute the Parent Node using some other library that requires the XPath String from the getParentXPath() method. This computation of the Parent Node node is turning out to be expensive as we need to do this for every difference encountered.

    Is there a way for us to access the reference of the ParentNode directly from the Comparison.Detail object (fetched by iterating through the Diff object)? If it's an expensive overhead, can such a feature be included at least when the target node is null?

    *A Snippet of the code where we are using this*
    .
    .
    .
    Node controlNode = comparison.getControlDetails().getTarget();
    if(controlNode == null) {
    XPath xpath = XPathFactory.newInstance().newXPath();
    Node parentNode =(Node) xpath.evaluate(comparison.getControlDetails().getParentXPath(), controlDoc, XPathConstants.NODE);
    .
    .
    .
    }

    Please advise if there is any better way to do this, or if we can expect a future version of XMLUnit to have this function.

    Thank you in advance!

     

    Last edit: MonishaM 2019-06-27
  • Stefan Bodewig

    Stefan Bodewig - 2019-07-02

    I don't think there is an easy way to do what you need without changing XMLUnit. You might gain a bit of performance by using a single XPathFactory and a single XPath instance, though.

    Something that might work is tracking what the difference engine is doing via a ComparisonListener and building a map of XPath to Node while the DOM is traversed.

    Adding the parent nodes in addition to the parent XPath would require some surgery to DOMDifferenceEngine but could probably be done without breaking the public API.

     

Log in to post a comment.

MongoDB Logo MongoDB