简体   繁体   中英

(Java)VTD-XML & Xpath Compering Nodes Children Elements

I have two xml files. One is reference(old) file and another one is test(new) file. Based on some rules supplied to me I have to check if something was removed from old model and then added to new one or check if something from old file was removed in new file.

I am using VTD-XML but DOM solution or any other that works with xpath will be really useful.

That is java code:

public void validateBooleanVTD2(PropertyRule prop, int i) throws XPathParseException, XPathEvalException,
        NavException {
    int n = -1;
    String xPath = prop.getEntitiesObjects().get(i).getxPath(); // eg. /people/man/attribute[not(key)]
    String propertyChecked = prop.getTag(); // eg. mandatory 
    VTDGen parseRef = new VTDGen();
    VTDGen parseTest = new VTDGen();
    parseRef.parseFile(ref, false);
    parseTest.parseFile(test, false);
    VTDNav navigateRef = parseRef.getNav();
    VTDNav navigateTest = parseTest.getNav();
    AutoPilot autopilotRef = new AutoPilot();
    AutoPilot autopilotTest = new AutoPilot();
    autopilotRef.bind(navigateRef);
    autopilotTest.bind(navigateTest);
    autopilotRef.selectXPath(xPath);
    //Instant start = Instant.now();

    while ((n = autopilotRef.evalXPath()) != -1) {
        int nameIndexRef = navigateRef.getAttrVal("name");
        String nameRef = navigateRef.toNormalizedString(nameIndexRef);
        autopilotTest.selectXPath(xPath + "[@name='" + nameRef + "']"); // eg. /people/man/attribute[not(key)][@name='John']
        int m = -1;
        while ((m = autopilotTest.evalXPath()) != -1) {

            if (navigateRef.toElement(VTDNav.FIRST_CHILD, propertyChecked) == false
                    && navigateTest.toElement(VTDNav.FIRST_CHILD, propertyChecked) == true) {// if it is in ref but not in test 

                System.out.println(nameRef + ":" + propertyChecked + ":Changed false to true");

                navigateRef.toElement(VTDNav.PARENT);
                navigateTest.toElement(VTDNav.PARENT);

            }

            else if (navigateRef.toElement(VTDNav.FIRST_CHILD, propertyChecked) == true
                    && navigateTest.toElement(VTDNav.FIRST_CHILD, propertyChecked) == false) { // if it is in test but not in ref
                System.out.println(nameRef + ":" + propertyChecked + ":Changed true to false");

                navigateRef.toElement(VTDNav.PARENT);
                navigateTest.toElement(VTDNav.PARENT);

            }

        }
        navigateTest.toElement(VTDNav.PARENT);
    }
    navigateRef.toElement(VTDNav.PARENT);

}

1)When xpath is done on ref file i get all attributes of man node:

/people/man/attribute[not(key)]

And I get value of name attribute.

2)Then I do another xpath on test file to get common attributes:

/people/man/attribute[not(key)][@name='attr1']

3) And then I have my if statements

PROBLEM: Without if statements i get all attributes from ref and test file There should be 29000 of them. When I trying to check if that node(attribute) has child node called mandatory for example I get 2 results back. But there should be much more where is the problem?

Ref File:

<people>
<man name="John">
    <attribute name="attr1">
    </mandatory>
    </attribute>
    <attribute name="attr2">
    </attribute>
</man>
<man name="Hans">
    <attribute name="attr3">
    </attribute>
</man>
<man name="Max">
    <attribute name="attr4">
    </attribute>
</man>

Test File:

<people>
<man name="John">
    <attribute name="attr1">

    </attribute>
    <attribute name="attr2">
    </attribute>
</man>
<man name="Hans">
    <attribute name="attr3">
    </attribute>
</man>
<man name="Max">
    <attribute name="attr4">
    </attribute>
</man>

So when I run my code I should get: attr1 changed from true to false

I found the solution to my problem:

public void validateBooleanVTD2(PropertyRule prop, int i) throws XPathParseException, XPathEvalException,
        NavException {
    int n = -1;
    String xPath = prop.getEntitiesObjects().get(i).getxPath(); // eg. /people/man/attribute[not(key)]
    String propertyChecked = prop.getTag(); // eg. mandatory 
    VTDGen parseRef = new VTDGen();
    VTDGen parseTest = new VTDGen();
    parseRef.parseFile(ref, false);
    parseTest.parseFile(test, false);
    VTDNav navigateRef = parseRef.getNav();
    VTDNav navigateTest = parseTest.getNav();
    AutoPilot autopilotRef = new AutoPilot();
    AutoPilot autopilotTest = new AutoPilot();
    autopilotRef.bind(navigateRef);
    autopilotTest.bind(navigateTest);
    autopilotRef.selectXPath(xPath);
    //Instant start = Instant.now();

    while ((n = autopilotRef.evalXPath()) != -1) {
        int nameIndexRef = navigateRef.getAttrVal("name");
        String nameRef = navigateRef.toNormalizedString(nameIndexRef);
        //System.out.println(navigateTest.toString(n + 2));
        //System.out.println(navigateTest.toString(n + 1));
        AutoPilot autopilotRefTestTag = new AutoPilot();
        AutoPilot autopilotTestTestTag = new AutoPilot();
        autopilotRefTestTag.bind(navigateRef);
        autopilotTestTestTag.bind(navigateTest);
        autopilotTestTestTag.selectXPath(xPath + "[@name='" + nameRef + "'][descendant::"+propertyChecked+"]"); // property in Test
        autopilotRefTestTag.selectXPath(xPath + "[@name='" + nameRef + "'][descendant::"+propertyChecked+"]"); // property in Ref
        if(autopilotRefTestTag.evalXPathToBoolean() == true && autopilotTestTestTag.evalXPathToBoolean() == false)
        {
            System.out.println(nameRef/* +":"+navigateRef.toString(n)+":"+propertyChecked + ":Updated:"+prop.getTrue2falseValue()+":Changed From True to False:"+prop.getTrue2falseDesc()*/);
        }
        if(autopilotRefTestTag.evalXPathToBoolean() == false && autopilotTestTestTag.evalXPathToBoolean() == true)
        {
            System.out.println(nameRef/* +":"+navigateRef.toString(n)+":"+propertyChecked + ":Updated:"+prop.getFalse2trueValue()+":Changed From False to True:"+prop.getFalse2trueDesc()*/);
        }

    }


}

I used another XPath in loop to check if in current Entity is entity that I am looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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