我有一个黄瓜xml报告,我想提取打开xml标签的属性中的信息
XML 报告如下所示
<testsuite failures="" name="" skipped="" tests="">
<testcase>
<skipped>
more tags but they are not relevant
</skipped>
</testcase>
</testsuite>
我希望能够从测试套件属性中获取信息,将它们保存为 java 对象,以便我可以将它们放入将发送到邮件列表的电子邮件正文中。
testsuite xml 标签中提供的信息是我所关心的。
解决这个问题的最佳方法是什么?我正在创建一个 Testsuite 类, 使用 @XStreamAsAttribute 注释我想要的属性的变量并获取它们 方式,但我相当不确定这是否正确。
我没有使用 xstream 的经验,所以尝试使用我可以在网上找到的信息。
使用 Java 和 XPath 的示例:
Document doc = DocumentBuilderFactory.newDefaultInstance()
.newDocumentBuilder()
.parse(...);
XPath xp = XPathFactory.newDefaultInstance().newXPath();
// Get values of known attributes
System.out.println("Name: " + xp.evaluate("/testsuite/@name", doc));
System.out.println("Tests: " + xp.evaluate("/testsuite/@tests", doc));
System.out.println("Failures: " + xp.evaluate("/testsuite/@failures", doc));
System.out.println("Skipped: " + xp.evaluate("/testsuite/@skipped", doc));
// Get name and values of arbitrary attributes
NodeList nl = (NodeList) xp.evaluate("/testsuite/@*", doc, XPathConstants.NODESET);
for (int i=0; i < nl.getLength(); ++i) {
Attr a = (Attr) nl.item(i);
System.out.printf("%s: %s\n", a.getName(), a.getValue());
}