简体   繁体   English

比较基于标记的两个xml作为唯一标记

[英]Compare two xml based on the tag as unique tag

I am developing application in which I have to compare 2 xml based on the IPADDRESS tag which is unique. 我正在开发应用程序,其中我必须根据唯一的IPADDRESS标记比较2个xml。 Ex: input1.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>

I tried using XMLUNIT java api but not able to do as we should filter only based on IPAddress tag.Please help and thanks in advance. 我尝试使用XMLUNIT java api,但不能做,因为我们应该仅基于IPAddress标记进行过滤。请事先帮助和感谢。

I assume you are collecting the differences between the two documents and want to emit the ones where the difference is CHILD_LOOKUP and only use those where the side representing input1 is null. 我假设您正在收集两个文档之间的差异,并且要发出差异为CHILD_LOOKUP并且仅使用那些表示input1为null的那一侧的文档。

What you need here is the exact same as the HTML Table example from https://github.com/xmlunit/user-guide/wiki/SelectingNodes - you want an ElementQualifier that usually matches XML elements by name but in the special case of the Device element you want to match those elements that have the same text content inside of their IPAddress child element. 这里需要的与来自https://github.com/xmlunit/user-guide/wiki/SelectingNodes的HTML Table示例完全相同-您需要一个ElementQualifier ,它通常按名称匹配XML元素,但在特殊情况下,您要匹配其IPAddress子元素内具有相同文本内容的那些元素的Device元素。

This would be something like 这就像

ElementSelectors.conditionalBuilder()
    .whenElementIsNamed("Device")
        .thenUse(ElementSelectors.byXPath("./IPAddress", ElementSelectors.byNameAndText))
    .elseUse(ElementSelectors.byName)
    .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM