0

我正在开发应用程序,我必须根据唯一的 IPADDRESS 标签比较 2 个 xml。例如:input1.xml

          <DeviceList>
                <Device>
                  <IPAddress>20.20.1.20</IPAddress>
                  <HostName>Device1</HostName>
                </Device>
                <Device>
                  <IPAddress>20.20.1.21</IPAddress>
                  <HostName>Device2</HostName>
                </Device>
                <Device>
                  <IPAddress>20.20.1.22</IPAddress>
                  <HostName>Device3</HostName>
                </Device>
           </DeviceList>

            inpu2.xml
          <DeviceList>
                <Device>
                  <IPAddress>20.20.1.23</IPAddress>
                  <HostName>Device1</HostName>
                </Device>
                <Device>
                  <IPAddress>20.20.1.21</IPAddress>
                  <HostName>Device3</HostName>
                </Device>
                <Device>
                  <IPAddress>20.20.1.22</IPAddress>
                  <HostName>Device3</HostName>
                </Device>
         </DeviceList>

Result should be two xmls 
output1 : ipadrees are present in input2.xml and not present in input.xml1


  <DeviceList>
      <Device>
          <IPAddress>10.20.1.23</IPAddress>
          <HostName>Device1</HostName>
      </Device>
  </DeviceList>

output2 : Remaining device list in input1.xml

 <DeviceList>
          <Device>
      <IPAddress>10.20.1.20</IPAddress>
      <HostName>Device1</HostName>
    </Device>
    <Device>
      <IPAddress>10.20.1.21</IPAddress>
      <HostName>Device2</HostName>
    </Device>
    <Device>
      <IPAddress>10.20.1.22</IPAddress>
      <HostName>Device3</HostName>
    </Device>
  </DeviceList>

我尝试使用 XMLUNIT java api,但无法做到,因为我们应该只基于 IPAddress 标签进行过滤。请提前帮助并感谢。

4

1 回答 1

0

我假设您正在收集两个文档之间的差异,并希望发出差异所在的文档,CHILD_LOOKUP并且仅使用表示 input1 的一侧为空的文档。

您在这里需要的与https://github.com/xmlunit/user-guide/wiki/SelectingNodes中的 HTML 表示例完全相同- 您需要一个ElementQualifier通常按名称匹配 XML 元素但在元素的特殊情况Device下您想要匹配那些在其IPAddress子元素内具有相同文本内容的元素。

这就像

ElementSelectors.conditionalBuilder()
    .whenElementIsNamed("Device")
        .thenUse(ElementSelectors.byXPath("./IPAddress", ElementSelectors.byNameAndText))
    .elseUse(ElementSelectors.byName)
    .build();
于 2019-01-11T12:31:48.873 回答