1

我有 2 个 XML 文档,如下所示

XML1

<root>
<element>
<attributes>
<a>1</a>
<b>y</b>
<c>n</c>
<attribute>
<attributes>
<a>2</a>
<b>j</b>
<c>m</c>
<attribute>
<attributes>
<a>3</a>
<b>y</b>
<c>n</c>
<attribute>
<attributes>
<a>4</a>
<b>y</b>
<c>n</c>
<attribute>
<element>
<element2>
</element2>
</root>

**XMl2**:
<root>
<element>
<attributes>
<a>1</a>
<b>y</b>
<c>n</c>
<attribute>
<attributes>
<a>3</a>
<b>y</b>
<c>n</c>
<attribute>
<attributes>
<a>4</a>
<b>y</b>
<c>n</c>
<attribute>
<element>
<element2>
</element2>
</root>

我只想在两个 XML 中的<attribute>第一个子元素 <a>文本相等时比较子元素

例如,我想比较XML1 的 where = 3 和 XML2 的<attribute>where = 3。<a><attribute><a>

目前我正在使用下面的代码。

Diff myDiff=null;

    DifferenceListener myDifferenceListener = new IgnoreTextAndAttributeValuesDifferenceListener();

    try {
        myDiff = new Diff(expected,actual);
        myDiff.overrideDifferenceListener(myDifferenceListener);
    } catch (SAXException | IOException e) {
        e.printStackTrace();
    }

   DetailedDiff myDiffd = new DetailedDiff(myDiff);
    List<Difference> allDifferences = myDiffd.getAllDifferences();

这给了我错误的比较。如果XML 中缺少<attribute>where <a>= 3,则此节点和下一个所有节点显示为不匹配。但我只希望这个节点不匹配。

任何帮助表示赞赏。

4

0 回答 0