1

我有 2 个 xml 文件,a.xml并且是一些节点值已更改的修改版本。现在我想检查我的代码,只有特定的节点值可以更改,而不是所有节点。我怎样才能在java中进行这个检查?我从用户那里得到这个 xml,如果他更改了我不应该允许的其他节点,他只能更改特定节点。下面是 xml 你可以看到b.xmlb.xmla.xml

<xt:path>HelloWorld/BusinessServices/Mytest</xt:path>两者都发生了变化这是有效的情况,因为他也发生了变化

<xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">40</xt:value>从 30 到 40 这是无效的。

一个.xml

<?xml version="1.0" encoding="UTF-8"?>
<cus:Customizations xmlns:cus="http://www.bea.com/wli/config/customizations" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xt="http://www.bea.com/wli/config/xmltypes">
  <cus:customization xsi:type="cus:EnvValueCustomizationType">
    <cus:description/>
    <cus:envValueAssignments>
      <xt:envValueType>Service URI Weight</xt:envValueType>
      <xt:location>0</xt:location>
      <xt:owner>
        <xt:type>BusinessService</xt:type>
        <xt:path>HelloWorld/BusinessServices/Test123</xt:path>
      </xt:owner>
      <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">0</xt:value>
    </cus:envValueAssignments>
    <cus:envValueAssignments>
      <xt:envValueType>Service URI</xt:envValueType>
      <xt:location>0</xt:location>
      <xt:owner>
        <xt:type>BusinessService</xt:type>
        <xt:path>HelloWorld/BusinessServices/Test123</xt:path>
      </xt:owner>
      <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">jejb:oracle:test</xt:value>
    </cus:envValueAssignments>
    <cus:envValueAssignments>
      <xt:envValueType>Service URI Table</xt:envValueType>
      <xt:location xsi:nil="true"/>
      <xt:owner>
        <xt:type>BusinessService</xt:type>
        <xt:path>HelloWorld/BusinessServices/Test123</xt:path>
      </xt:owner>
      <xt:value>
        <tableElement xmlns="http://www.bea.com/wli/sb/transports">
          <URI>jejb:oracle:test</URI>
          <weight>0</weight>
        </tableElement>
      </xt:value>
    </cus:envValueAssignments>
    <cus:envValueAssignments>
      <xt:envValueType>Service Retry Count</xt:envValueType>
      <xt:location xsi:nil="true"/>
      <xt:owner>
        <xt:type>BusinessService</xt:type>
        <xt:path>HelloWorld/BusinessServices/Test123</xt:path>
      </xt:owner>
      <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">0</xt:value>
    </cus:envValueAssignments>
    <cus:envValueAssignments>
      <xt:envValueType>Service Retry Iteration Interval</xt:envValueType>
      <xt:location xsi:nil="true"/>
      <xt:owner>
        <xt:type>BusinessService</xt:type>
        <xt:path>HelloWorld/BusinessServices/Test123</xt:path>
      </xt:owner>
      <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">30</xt:value>
    </cus:envValueAssignments>
  </cus:customization>
  <cus:customization xsi:type="cus:FindAndReplaceCustomizationType">
    <cus:description/>
    <cus:query>
      <xt:resourceTypes>BusinessService</xt:resourceTypes>
      <xt:envValueTypes>Service Retry Iteration Interval</xt:envValueTypes>
      <xt:envValueTypes>Service URI Weight</xt:envValueTypes>
      <xt:envValueTypes>Service Retry Count</xt:envValueTypes>
      <xt:envValueTypes>Service URI</xt:envValueTypes>
      <xt:refsToSearch xsi:type="xt:ResourceRefType">
        <xt:type>BusinessService</xt:type>
        <xt:path>HelloWorld/BusinessServices/Test123</xt:path>
      </xt:refsToSearch>
      <xt:includeOnlyModifiedResources>false</xt:includeOnlyModifiedResources>
      <xt:searchString>Search String</xt:searchString>
      <xt:isCompleteMatch>false</xt:isCompleteMatch>
    </cus:query>
    <cus:replacement>Replacement String</cus:replacement>
  </cus:customization>
  <cus:customization xsi:type="cus:ReferenceCustomizationType">
    <cus:description/>
    <cus:refsToBeConsidered xsi:type="xt:ResourceRefType">
      <xt:type>BusinessService</xt:type>
      <xt:path>HelloWorld/BusinessServices/Test123</xt:path>
    </cus:refsToBeConsidered>
    <cus:externalReferenceMap>
      <xt:oldRef>
        <xt:type>ForeignJNDIProvider</xt:type>
        <xt:path>System/JNDI Providers/oracle</xt:path>
      </xt:oldRef>
      <xt:newRef>
        <xt:type>ForeignJNDIProvider</xt:type>
        <xt:path>System/JNDI Providers/oracle</xt:path>
      </xt:newRef>
    </cus:externalReferenceMap>
    <cus:externalReferenceMap>
      <xt:oldRef>
        <xt:type>Archive</xt:type>
        <xt:path>dev/HelloOsbEjbClient</xt:path>
      </xt:oldRef>
      <xt:newRef>
        <xt:type>Archive</xt:type>
        <xt:path>dev/HelloOsbEjbClient</xt:path>
      </xt:newRef>
    </cus:externalReferenceMap>
  </cus:customization>
</cus:Customizations>

b.xml

<?xml version="1.0" encoding="UTF-8"?>
<cus:Customizations xmlns:cus="http://www.bea.com/wli/config/customizations" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xt="http://www.bea.com/wli/config/xmltypes">
  <cus:customization xsi:type="cus:EnvValueCustomizationType">
    <cus:description/>
    <cus:envValueAssignments>
      <xt:envValueType>Service URI Weight</xt:envValueType>
      <xt:location>0</xt:location>
      <xt:owner>
        <xt:type>BusinessService</xt:type>
        <xt:path>HelloWorld/BusinessServices/Test123</xt:path>
      </xt:owner>
      <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">0</xt:value>
    </cus:envValueAssignments>
    <cus:envValueAssignments>
      <xt:envValueType>Service URI</xt:envValueType>
      <xt:location>0</xt:location>
      <xt:owner>
        <xt:type>BusinessService</xt:type>
        <xt:path>HelloWorld/BusinessServices/Test123</xt:path>
      </xt:owner>
      <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">jejb:oracle:test</xt:value>
    </cus:envValueAssignments>
    <cus:envValueAssignments>
      <xt:envValueType>Service URI Table</xt:envValueType>
      <xt:location xsi:nil="true"/>
      <xt:owner>
        <xt:type>BusinessService</xt:type>
        <xt:path>HelloWorld/BusinessServices/Test123</xt:path>
      </xt:owner>
      <xt:value>
        <tableElement xmlns="http://www.bea.com/wli/sb/transports">
          <URI>jejb:oracle:test</URI>
          <weight>0</weight>
        </tableElement>
      </xt:value>
    </cus:envValueAssignments>
    <cus:envValueAssignments>
      <xt:envValueType>Service Retry Count</xt:envValueType>
      <xt:location xsi:nil="true"/>
      <xt:owner>
        <xt:type>BusinessService</xt:type>
        <xt:path>HelloWorld/BusinessServices/Test123</xt:path>
      </xt:owner>
      <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">0</xt:value>
    </cus:envValueAssignments>
    <cus:envValueAssignments>
      <xt:envValueType>Service Retry Iteration Interval</xt:envValueType>
      <xt:location xsi:nil="true"/>
      <xt:owner>
        <xt:type>BusinessService</xt:type>
        <xt:path>HelloWorld/BusinessServices/Mytest</xt:path>
      </xt:owner>
      <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">40</xt:value>
    </cus:envValueAssignments>
  </cus:customization>
  <cus:customization xsi:type="cus:FindAndReplaceCustomizationType">
    <cus:description/>
    <cus:query>
      <xt:resourceTypes>BusinessService</xt:resourceTypes>
      <xt:envValueTypes>Service Retry Iteration Interval</xt:envValueTypes>
      <xt:envValueTypes>Service URI Weight</xt:envValueTypes>
      <xt:envValueTypes>Service Retry Count</xt:envValueTypes>
      <xt:envValueTypes>Service URI</xt:envValueTypes>
      <xt:refsToSearch xsi:type="xt:ResourceRefType">
        <xt:type>BusinessService</xt:type>
        <xt:path>HelloWorld/BusinessServices/MyTest</xt:path>
      </xt:refsToSearch>
      <xt:includeOnlyModifiedResources>false</xt:includeOnlyModifiedResources>
      <xt:searchString>Search String</xt:searchString>
      <xt:isCompleteMatch>false</xt:isCompleteMatch>
    </cus:query>
    <cus:replacement>Replacement String</cus:replacement>
  </cus:customization>
  <cus:customization xsi:type="cus:ReferenceCustomizationType">
    <cus:description/>
    <cus:refsToBeConsidered xsi:type="xt:ResourceRefType">
      <xt:type>BusinessService</xt:type>
      <xt:path>HelloWorld/BusinessServices/Test123</xt:path>
    </cus:refsToBeConsidered>
    <cus:externalReferenceMap>
      <xt:oldRef>
        <xt:type>ForeignJNDIProvider</xt:type>
        <xt:path>System/JNDI Providers/oracle</xt:path>
      </xt:oldRef>
      <xt:newRef>
        <xt:type>ForeignJNDIProvider</xt:type>
        <xt:path>System/JNDI Providers/oracle</xt:path>
      </xt:newRef>
    </cus:externalReferenceMap>
    <cus:externalReferenceMap>
      <xt:oldRef>
        <xt:type>Archive</xt:type>
        <xt:path>dev/HelloOsbEjbClient</xt:path>
      </xt:oldRef>
      <xt:newRef>
        <xt:type>Archive</xt:type>
        <xt:path>dev/HelloOsbEjbClient</xt:path>
      </xt:newRef>
    </cus:externalReferenceMap>
  </cus:customization>
</cus:Customizations>
4

2 回答 2

2

尝试用于 Java 的 XMLUnit

XMLUnit for Java 提供了两个 JUnit 扩展类,XMLAssert 和 XMLTestCase,以及一组支持类(例如 Diff、DetailedDiff、Transform、SimpleXpathEngine、Validator、NodeTest),这些类允许对以下内容进行断言:

  • 两段 XML 的区别

  • 使用 XSLT 转换一段 XML 的结果

  • 对一段 XML 的 XPath 表达式求值

  • 一段 XML 的有效性

  • 由 DOM Traversal 公开的一段 XML 中的各个节点

    XMLUnit for Java 还可以将 HTML 内容(甚至是格式错误的 HTML)视为有效的 XML,以允许对网页内容进行这些断言。

可以在此处找到另一个 DocumentBuilder 解决方案。

于 2013-05-22T11:43:50.537 回答
2

嗯,您可以使用 DocumentBuilder 阅读这两个文件。将节点存储在例如一些列表中,您可以在阅读它们后进行比较:

DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fac.newDocumentBuilder();
Document doc = builder.parse(new File("a.xml");

之后,您可以从文档中获取所有节点(存储在 NodeList 中)

NodeList list = doc.getElementsbyName("someelement"); //just for an example

希望我能帮到你一点

于 2013-05-22T11:27:17.357 回答