[英](Java)VTD-XML & Xpath Compering Nodes Children Elements
I have two xml files. 我有两个xml文件。 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. 我正在使用VTD-XML,但是DOM解决方案或任何其他与xpath一起使用的解决方案将非常有用。
That is java code: 那是java代码:
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: 1)当在引用文件上完成xpath时,我得到了man节点的所有属性:
/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: 2)然后我在测试文件上执行另一个xpath以获得通用属性:
/people/man/attribute[not(key)][@name='attr1']
3) And then I have my if statements 3)然后我有if语句
PROBLEM: Without if statements i get all attributes from ref and test file There should be 29000 of them. 问题:如果没有if语句,我将从ref和测试文件中获取所有属性,应该有29000个属性。 When I trying to check if that node(attribute) has child node called mandatory for example I get 2 results back. 例如,当我尝试检查该节点(属性)是否具有称为强制性的子节点时,我得到了2个结果。 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 因此,当我运行代码时,我应该得到:attr1从true更改为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. 我在循环中使用了另一个XPath来检查当前Entity中是否是我要寻找的实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.