[英](Java)VTD-XML & Xpath Compering Nodes Children Elements
我有兩個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時,我得到了man節點的所有屬性:
/people/man/attribute[not(key)]
我得到名稱屬性的值。
2)然后我在測試文件上執行另一個xpath以獲得通用屬性:
/people/man/attribute[not(key)][@name='attr1']
3)然后我有if語句
問題:如果沒有if語句,我將從ref和測試文件中獲取所有屬性,應該有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來檢查當前Entity中是否是我要尋找的實體。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.