作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个 xml 文件。一种是引用(旧)文件,另一种是测试(新)文件。根据提供给我的一些规则,我必须检查是否从旧模型中删除了某些内容,然后将其添加到新模型中,或者检查旧文件中的某些内容是否在新文件中删除了。
我正在使用 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)当 xpath 在 ref 文件上完成时,我得到了 man 节点的所有属性:
/people/man/attribute[not(key)]
我得到了 name 属性的值。
2)然后我在测试文件上执行另一个 xpath 以获得公共(public)属性:
/people/man/attribute[not(key)][@name='attr1']
3)然后我就有了 if 语句
问题:没有 if 语句,我从引用和测试文件中获取所有属性应该有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
最佳答案
我找到了问题的解决方案:
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 来检查当前实体中是否是我正在查找的实体。
关于java - (Java)VTD-XML 和 Xpath 比较节点子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24289323/
我是一名优秀的程序员,十分优秀!