【发布时间】:2018-11-06 06:23:52
【问题描述】:
我想使用 JAVA 连接来自 xml 文件的多个标记值。 .xml 看起来像这样:
<test>
<testcase>
<teststep> row 1 </teststep>
<teststep> row 2 </teststep>
<teststep> row 3 </teststep>
<title> Frist Test </title>
</testcase>
</test>
<test>
<testcase>
<teststep> row 20 </teststep>
<teststep> row 10 </teststep>
<teststep> row 30 </teststep>
<title> Second Test </title>
</testcase>
</test>
结果应该是这样的:
row 1 row 2 row 3
row 10 row 20 row 30
应该有2个变量。
我试过了:
NodeList nodeList5 = doc.getElementsByTagName("teststep");
for (int x = 0, size = nodeList5.getLength(); x < size; x++) {
description = description + nodeList5.item(x).getTextContent();
}
System.out.println("Test Description: " + description);
但我得到的只是:第 1 行 第 2 行 第 3 行 第 10 行 第 20 行 第 30 行,只有一个变量。
【问题讨论】:
-
您尝试的代码是 JavaScript。要在 Java 中处理 XML,不要重新发明轮子 - 查找 JAXB 和 JAXP。
标签: java xml concatenation