英文:
XMLUnit-2 ignore certain nested XML elements
问题
我的XML结构比较复杂,我需要忽略某些比较中的“entry”元素,我该如何实现?
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE ResourceObject PUBLIC "my_corp.dtd" "my_corp.dtd">
<ResourceObject displayName="TESTNGAD\AggUserFSP test" identity="CN=AggUserFSP test,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local" objectType="account" uuid="{97182a65-61f2-443c-b0fa-477d0821d8c4}">
<Attributes>
<Map>
<entry key="accountFlags">
<value>
<List>
<String>Normal User Account</String>
<String>Password Cannot Expire</String>
</List>
</value>
</entry>
<entry key="homePhone" value="6555"/>
<entry key="l" value="Pune"/>
<entry key="memberOf">
<value>
<List>
<String>CN=FSPGRP2,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL</String>
<String>CN=FSPGRP1,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL</String>
<String>CN=LocalAggFrame,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local</String>
</List>
</value>
</entry>
<entry key="objectClass">
<value>
<List>
<String>top</String>
<String>person</String>
<String>organizationalPerson</String>
<String>user</String>
</List>
</value>
</entry>
<entry key="sn" value="test"/>
<entry key="st" value="MH"/>
<entry key="streetAddress" value="SB ROAD"/>
<entry key="title" value="QA"/>
<entry key="userPrincipalName" value="AggUserFSP test@TestNGAD.local"/>
</Map>
</Attributes>
</ResourceObject>
我尝试了以下代码:
Diff diff = DiffBuilder
.compare(control)
.withTest(test)
.checkForSimilar().checkForIdentical()
.normalizeWhitespace()
.ignoreComments()
.ignoreWhitespace()
.withNodeFilter(node -> !(node.getNodeName().equals("accountFlags") ||
node.getNodeName().equals("homePhone"))).build();
但是它没有起作用。在这里,我应该如何忽略一些“XML entry”元素呢?
英文:
My XML is little complex and I've to ignore certain entry
from the comparison, how would I able to achieve it ?
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE ResourceObject PUBLIC "my_corp.dtd" "my_corp.dtd">
<ResourceObject displayName="TESTNGAD\AggUserFSP test" identity="CN=AggUserFSP test,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local" objectType="account" uuid="{97182a65-61f2-443c-b0fa-477d0821d8c4}">
<Attributes>
<Map>
<entry key="accountFlags">
<value>
<List>
<String>Normal User Account</String>
<String>Password Cannot Expire</String>
</List>
</value>
</entry>
<entry key="homePhone" value="6555"/>
<entry key="l" value="Pune"/>
<entry key="memberOf">
<value>
<List>
<String>CN=FSPGRP2,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL</String>
<String>CN=FSPGRP1,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL</String>
<String>CN=LocalAggFrame,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local</String>
</List>
</value>
</entry>
<entry key="objectClass">
<value>
<List>
<String>top</String>
<String>person</String>
<String>organizationalPerson</String>
<String>user</String>
</List>
</value>
</entry>
<entry key="sn" value="test"/>
<entry key="st" value="MH"/>
<entry key="streetAddress" value="SB ROAD"/>
<entry key="title" value="QA"/>
<entry key="userPrincipalName" value="AggUserFSP test@TestNGAD.local"/>
</Map>
</Attributes>
</ResourceObject>
I tried
Diff diff = DiffBuilder
.compare(control)
.withTest(test)
.checkForSimilar().checkForIdentical()
.normalizeWhitespace()
.ignoreComments()
.ignoreWhitespace()
.withNodeFilter(node -> !(node.getNodeName().equals("accountFlags") ||
node.getNodeName().equals("homePhone"))).build();
But, it is not working. How should I ignore some XML entry
here?
答案1
得分: 0
"accountFlags"和"homePhone"都不是元素名称,因此我的筛选条件不会匹配任何内容。
除非满足以下所有条件,NodeFilter必须返回true:
- 节点实际上是一个元素
- 节点具有名为"key"的属性
- 此属性的值为"accountFralgs"或"homePhone"
```java
private boolean filter(final Node n) {
if (n instanceof Element) {
final String attrValue = ((Element) n).getAttibute("key");
// 如果属性缺失,attrValue将为空字符串
return !("accountFlags".equals(attrValue) || "homePhone".equals(attrValue));
}
return true;
}
英文:
Neither "accountFlags" nor "homePhone" are element names, so my filter will not match anything.
The NodeFilter must return true unless all of the following conditions are met
- node is actually an Element
- node has an attribute named "key"
- the value of this attribute is either "accountFralgs" or "homePhone"
private boolean filter(final Node n) {
if (n instanceof Element) {
final String attrValue = ((Element) n).getAttibute("key");
// attrValue is th eempty string if the attribute is missing
return !("accountFlags".equals(attrValue) || "homePhone".equals(attrValue));
}
return true;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论