首页 > 解决方案 > 根据本地 XML 文件验证 XML 文件的最佳方法是什么?

问题描述

我正在开发一些测试框架。我想用本地数据验证极其复杂的 XML 响应。

我认为有 CSV 格式的本地数据并进行了一些验证,但我发现这个框架的局限性是我无法验证复杂数据,例如

<?xml version='1.0' encoding='UTF-8'?>
 <!DOCTYPE ResourceObject PUBLIC "abc_corp.dtd" "abc_corp.dtd">
 <ResourceObject displayName="abcd" identity="pqr" objectType="account" uuid="123456">
   <Attributes>
     <Map>
       <entry key="memberOf"/>
       <entry key="objectClass">
         <value>
           <List>
             <String>top</String>
             <String>person</String>
             <String>organizationalPerson</String>
             <String>user</String>
           </List>
         </value>
       </entry>
       <entry key="objectSid" value="S-1-5"/>
       <entry key="objectType" value="user"/>
          <value>
           <List>
             <Permission rights="allow:elasticmapreduce:Describe*" target="*"/>
             <Permission rights="allow:elasticmapreduce:List*" target="*"/>
             <Permission rights="allow:elasticmapreduce:ViewEventsFromAllClustersInConsole" target="*"/>
             <Permission rights="allow:s3:GetObject" target="*"/>
             <Permission rights="allow:s3:ListAllMyBuckets" target="*"/>
             <Permission rights="allow:s3:ListBucket" target="*"/>
             <Permission rights="allow:sdb:Select" target="*"/>
             <Permission rights="allow:cloudwatch:GetMetricStatistics" target="*"/>
           </List>
         </value>
       </entry>
     </Map>
   </Attributes>
 </ResourceObject>

下面的上面的XML对象entry是难以用CSV格式表示的东西

       <entry key="objectType" value="user"/>
          <value>
           <List>
             <Permission rights="allow:elasticmapreduce:Describe*" target="*"/>
             <Permission rights="allow:elasticmapreduce:List*" target="*"/>
             <Permission rights="allow:elasticmapreduce:ViewEventsFromAllClustersInConsole" target="*"/>
             <Permission rights="allow:s3:GetObject" target="*"/>
             <Permission rights="allow:s3:ListAllMyBuckets" target="*"/>
             <Permission rights="allow:s3:ListBucket" target="*"/>
             <Permission rights="allow:sdb:Select" target="*"/>
             <Permission rights="allow:cloudwatch:GetMetricStatistics" target="*"/>
           </List>
         </value>
       </entry>

或持有list of maps等的 XML 数据。

是否有任何可用的框架、库可以验证如此复杂的 XML 数据?

标签: javaxmlunit-testingautomation

解决方案


看看这些以供参考..

  1. https://javarevisited.blogspot.com/2017/04/how-to-compare-two-xml-files-in-java.htm
  2. 在 Java 中比较 2 个 XML 文档的最佳方法
    //creating Diff instance to compare two XML files 
    Diff xmlDiff = new Diff(source, target); 

    //for getting detailed differences between two xml files 
    DetailedDiff detailXmlDiff = new DetailedDiff(xmlDiff);

希望这可以帮助。


推荐阅读