我有两个xml文件。一个是引用(旧)文件,另一个是test(新)文件。 基于提供给我的一些规则,我必须检查是否从旧模型中删除了某些内容然后添加到新模型中或检查旧文件中的某些内容是否已在新文件中删除。
我正在使用VTD-XML,但DOM解决方案或任何其他适用于xpath的解决方案都非常有用。
那是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)当在ref文件上完成xpath时,我得到man节点的所有属性:
/people/man/attribute[not(key)]
我得到了name属性的价值。
2)然后我在测试文件上执行另一个xpath以获取公共属性:
/people/man/attribute[not(key)][@name='attr1']
3)然后我有我的if语句
问题:如果没有if语句,我会从ref和test文件中获取所有属性 应该有29000个。当我试图检查该节点(属性)是否具有被称为强制的子节点时,我得到了2个结果。但问题应该在哪里呢?
参考文件:
<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>
测试文件:
<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>
因此,当我运行我的代码时,我应该得到: attr1从true变为false
答案 0 :(得分:2)
我找到了解决问题的方法:
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()*/);
}
}
}
我在循环中使用了另一个XPath来检查当前实体中是否是我正在寻找的实体。