比较两个相似的 XML 数据与 Java 中的无序元素/属性 [英] Comparing two similar XML data with unordered elements/attributes in Java
问题描述
我正在寻找一种可以比较两个 XML 数据的 API.我已经尝试过 XMLUnit 2,但找不到让它在我的示例中正常工作的方法.你能给我一个适合我需要的例子吗?
I'm looking for an API which compares two XML data. I've tried XMLUnit 2 but couldn't find a way to make it work properly with my example. Could you give me an example which works for my need?
我的第一个 XML 数据 xml1
:
My first XML data 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
:
My second XML data 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
的元素(loc
和lastmod
)可能无法排序- 忽略空格
- Same size (here 3 children)
urlset
's child nodes (url
) may not be orderedurl
's elements (loc
andlastmod
) may not be ordered- White spaces are ignored
寻找返回 true
的 API,例如:
Looking for an API which returns true
like:
XMLUtils.isSimilar(xml1, xml2);
我使用 XMLUnit 2 的失败尝试(尝试使用多个NodeMatcher"):
My unsuccessful attempts with XMLUnit 2 (tried with multiple "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();
}
}
}
我在这里复制了我们在我的项目中使用的方法.
I copying here a method we are using in my project.
你能试试吗,如果你遇到任何问题,请告诉我.我可以自己再试试.
Could you try it and let me know if you are facing any issue. I can try by myself again.
谢谢
这篇关于比较两个相似的 XML 数据与 Java 中的无序元素/属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!