XMLUnit-2 忽略特定的嵌套XML元素

huangapple go评论71阅读模式
英文:

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 ?

&lt;?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?&gt;
&lt;!DOCTYPE ResourceObject PUBLIC &quot;my_corp.dtd&quot; &quot;my_corp.dtd&quot;&gt;
&lt;ResourceObject displayName=&quot;TESTNGAD\AggUserFSP test&quot; identity=&quot;CN=AggUserFSP test,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local&quot; objectType=&quot;account&quot; uuid=&quot;{97182a65-61f2-443c-b0fa-477d0821d8c4}&quot;&gt;
   &lt;Attributes&gt;
     &lt;Map&gt;
       &lt;entry key=&quot;accountFlags&quot;&gt;
         &lt;value&gt;
           &lt;List&gt;
             &lt;String&gt;Normal User Account&lt;/String&gt;
             &lt;String&gt;Password Cannot Expire&lt;/String&gt;
           &lt;/List&gt;
         &lt;/value&gt;
       &lt;/entry&gt;
       &lt;entry key=&quot;homePhone&quot; value=&quot;6555&quot;/&gt;
       &lt;entry key=&quot;l&quot; value=&quot;Pune&quot;/&gt;
       &lt;entry key=&quot;memberOf&quot;&gt;
         &lt;value&gt;
           &lt;List&gt;
             &lt;String&gt;CN=FSPGRP2,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL&lt;/String&gt;
             &lt;String&gt;CN=FSPGRP1,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL&lt;/String&gt;
             &lt;String&gt;CN=LocalAggFrame,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local&lt;/String&gt;
           &lt;/List&gt;
         &lt;/value&gt;
       &lt;/entry&gt;
       &lt;entry key=&quot;objectClass&quot;&gt;
         &lt;value&gt;
           &lt;List&gt;
             &lt;String&gt;top&lt;/String&gt;
             &lt;String&gt;person&lt;/String&gt;
             &lt;String&gt;organizationalPerson&lt;/String&gt;
             &lt;String&gt;user&lt;/String&gt;
           &lt;/List&gt;
         &lt;/value&gt;
       &lt;/entry&gt;
       &lt;entry key=&quot;sn&quot; value=&quot;test&quot;/&gt;
       &lt;entry key=&quot;st&quot; value=&quot;MH&quot;/&gt;
       &lt;entry key=&quot;streetAddress&quot; value=&quot;SB ROAD&quot;/&gt;
       &lt;entry key=&quot;title&quot; value=&quot;QA&quot;/&gt;
       &lt;entry key=&quot;userPrincipalName&quot; value=&quot;AggUserFSP test@TestNGAD.local&quot;/&gt;
     &lt;/Map&gt;
   &lt;/Attributes&gt;
 &lt;/ResourceObject&gt;

I tried

Diff diff = DiffBuilder
             .compare(control)
             .withTest(test)
             .checkForSimilar().checkForIdentical()
             .normalizeWhitespace()
             .ignoreComments()
             .ignoreWhitespace()
             .withNodeFilter(node -&gt; !(node.getNodeName().equals(&quot;accountFlags&quot;) ||
                            node.getNodeName().equals(&quot;homePhone&quot;))).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(&quot;key&quot;);
            // attrValue is th eempty string if the attribute is missing
            return !(&quot;accountFlags&quot;.equals(attrValue) || &quot;homePhone&quot;.equals(attrValue));
        }
        return true;
    }

huangapple
  • 本文由 发表于 2020年4月10日 19:30:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61139377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定