(Java)VTD-XML&;Xpath竞争节点子元素
我有两个xml文件。一个是参考(旧)文件,另一个是测试(新)文件。 根据提供给我的一些规则,我必须检查是否有东西从旧模型中删除,然后添加到新模型中,或者检查是否有东西从旧文件中删除到新文件中 我使用的是VTD-XML,但DOM解决方案或任何其他使用xpath的解决方案都非常有用 这就是java代码:(Java)VTD-XML&;Xpath竞争节点子元素,java,xml,dom,xpath,vtd-xml,Java,Xml,Dom,Xpath,Vtd Xml,我有两个xml文件。一个是参考(旧)文件,另一个是测试(新)文件。 根据提供给我的一些规则,我必须检查是否有东西从旧模型中删除,然后添加到新模型中,或者检查是否有东西从旧文件中删除到新文件中 我使用的是VTD-XML,但DOM解决方案或任何其他使用xpath的解决方案都非常有用 这就是java代码: public void validateBooleanVTD2(PropertyRule prop, int i) throws XPathParseException, XPathEvalExce
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和测试文件中获取所有属性
应该有29000个。例如,当我试图检查该节点(属性)是否有名为mandatory的子节点时,我得到了两个结果。但应该有更多问题在哪里
参考文件:
<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我找到了问题的解决方案:
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来检查当前实体中是否有我正在寻找的实体。您的问题不清楚。您将显示一个文件,其中包含一个
必需的子项,另一个不包含该子项。这一切意味着什么?您还有一个XPath表达式/people/man/attribute[not(key)][@name='John']
,用于测试子
元素。它在哪里?为什么重要?我想找到两个文件的公共属性(元素),然后检查是否在新文件中添加或删除了调用的子项(例如)。对不起,我编辑了文章,所以现在是正确的,我想我可以帮上忙吗?是的。当我执行navigateRef.toElement(VTDNav.FIRST\u CHILD,propertyChecked)==false时,我将全局光标移动到
,但随后执行navigateRef.toElement(VTDNav.PARENT)代码>返回属性,但出于某种原因,我没有迭代剩余的元素