为什么我从DOM解析器中获取额外的节点内容

时间:2015-08-24 18:10:02

标签: java parsing dom

我想从modules节点获取文本上下文,但它会从树中返回额外的值。

<modules>New Enterprise Development 
    <credits>15</credits>
    <level>M</level>   
    <core_or_optional>core</core_or_optional>
</modules>
<modules>Computing Law, Contracts and Professional Responsibility
    <credits>15</credits>
    <level>M</level>
    <core_or_optional>core</core_or_optional>
</modules>

解析代码:

for (int j=0; j<NodelistB.getLength(); j++){
    Node xPathNode = NodelistB.item(j);
    Element xPath = (Element)xPathNode;
    targetNodeContent = xPath.getTextContent();
    targetNode = xPath.getTagName();
    System.out.println(targetNodeContent) ; 
    System.out.println() ;      
}

我希望返回值为:

New Enterprise Development
Computing Law, Contracts and Professional Responsibility

但它打印出以下内容:

New Enterprise Development 
15
M
core                 

Computing Law, Contracts and Professional Responsibility
15
M
core

1 个答案:

答案 0 :(得分:0)

阅读getTextContent()的{​​{3}}:

  

此属性返回此节点及其后代的文本内容。