我正在寻找比较两个XML数据的API。我已经尝试过XMLUnit 2,但找不到与我的示例正常运行的方法。你能给我一个适合我需要的例子吗?

我的第一个XML数据xml1

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<urlset xmlns="http://www.sitemap.org/schemas/sitemap/0.9">
    <url>
        <loc>a1/</loc>
        <lastmod>a2</lastmod>
    </url>
    <url>
        <loc>b1</loc>
        <lastmod>b2</lastmod>
    </url>
    <url>
        <loc>c1</loc>
        <lastmod>c2</lastmod>
    </url>
</urlset>


我的第二个XML数据xml2

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<urlset xmlns="http://www.sitemap.org/schemas/sitemap/0.9">
    <url><lastmod>b2</lastmod><loc>b1</loc></url>
    <url>
        <lastmod>c2</lastmod>
        <loc>c1</loc>
    </url>
    <url>
        <loc>a1/</loc>
        <lastmod>a2</lastmod>
    </url>
</urlset>


注意:


相同大小(此处为3个孩子)
urlset的子节点(url)可能未排序
url的元素(loclastmod)可能不排序
空格被忽略


寻找一个返回true的API,例如:

XMLUtils.isSimilar(xml1, xml2);


我对XMLUnit 2的尝试失败(尝试了多个“ NodeMatcher”):

// Attempt with XmlAssert.assertThat:
XmlAssert.assertThat(xml1)
    .and(xml2)
    .ignoreChildNodesOrder()
    .ignoreWhitespace()
    .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
    .areSimilar();

// Attempt with Diff
Diff myDiff = DiffBuilder.compare(xml1)
    .withTest(xml2)
    .ignoreWhitespace()
    .checkForSimilar()
    .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
     .build();
myDiff.getDifferences();

最佳答案

您可以尝试如下

public class XMLUtils {
    private static DocumentBuilderFactory documentBuilderFactory;
    private static DocumentBuilder documentBuilder;
    private static TransformerFactory transformerFactory;
    private static Transformer transformer;
    private static Document emptyDoc;

    public XMLUtils() {
    }

    public XMLCompareResult compare(File expectedFile, File actualFile, boolean ignoreWhiteSpace) throws FileNotFoundException, SAXException, IOException {
        FileInputStream expInpStream = new FileInputStream(expectedFile);
        FileInputStream actualInpStream = new FileInputStream(actualFile);
        Diff myDiff = null;
        if (ignoreWhiteSpace) {
            myDiff = DiffBuilder.compare(expInpStream).withTest(actualInpStream).checkForSimilar().ignoreWhitespace().withNodeMatcher(new DefaultNodeMatcher(new ElementSelector[]{ElementSelectors.byNameAndAllAttributes})).build();
        } else {
            myDiff = DiffBuilder.compare(expInpStream).withTest(actualInpStream).checkForSimilar().withNodeMatcher(new DefaultNodeMatcher(new ElementSelector[]{ElementSelectors.byNameAndAllAttributes})).build();
        }

        XMLResultUtil xmlr = new XMLResultUtil();
        XMLCompareResult xs = xmlr.prepareXMLCompareResult(myDiff.getDifferences());
        return xs;
    }

    static {
        try {
            documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
            transformerFactory = TransformerFactory.newInstance();
            transformer = transformerFactory.newTransformer();
            emptyDoc = documentBuilder.newDocument();
        } catch (ParserConfigurationException var1) {
            var1.printStackTrace();
        } catch (TransformerConfigurationException var2) {
            var2.printStackTrace();
        }

    }
}


我在这里复制我们在项目中使用的方法。

您能否尝试一下,让我知道您是否遇到任何问题。我可以自己再试一次。

谢谢

07-28 00:49