我有一个这样的 XML 文件:
<book>
<chapter name="chap1">
<section name="c1sec1">
<paragraph name="title1">sample</paragraph>
</section>
<section name="c1sec2">
<paragraph name="title2">sample1</paragraph>
</section>
</chapter>
<chapter name="chap2">
<section name="c2sec1">
<paragraph name="title3">sample2</paragraph>
</section>
</chapter>
<chapter name="chap3">
<section name="c3sec1">
<paragraph name="title4">sample3</paragraph>
</section>
</chapter>
</book>
我想从特定章节中提取部分。假设从“chap1”,输出应该是这样的:
c1sec1
c1sec2
我编写了以下代码来实现这一点:
NodeList nodeList = doc.getElementsByTagName("chapter");
for (int i = 0; i < nodeList.getLength(); i++) {
Node n = nodeList.item(i);
NamedNodeMap m = n.getAttributes();
if(m.getNamedItem("name").getTextContent() == "chap1")
{
System.out.println(n.getFirstChild().getNodeValue());
}
}
我哪里错了?