XPathは正しく、正常に機能します。問題は
list.item(i).getChildNodes().item(0).getTextContent());
XPathに一致するノードの最初の子ノードを取得します。スペースのあるXMLの場合は直後のスペースですが、スペース<employee>
のないXMLの場合は<name>
要素です。
言い換えると、スペースがある場合、最初のemployee
要素の子ノードは次のようになります(1行に1つ)。
[spaces]
<name> . . . </name>
[spaces]
<company-no> . . . </company-no>
[spaces]
<chunk-id> . . .</chunk-id>
スペースがない場合は次のようになります。
<name> . . . </name>
<company-no> . . . </company-no>
<chunk-id> . . .</chunk-id>
したがって、最初のケースでは、必要な子ノードは1、3、および5であり、2番目のケースでは0、1、および2です。
このコードを変更する必要があると思います。
System.out.println("Name: " +list.item(i).getChildNodes().item(0).getTextContent());
System.out.println("Company: "+list.item(i).getChildNodes().item(1).getTextContent());
System.out.println("Chunk: "+list.item(i).getChildNodes().item(2).getTextContent());
他のXPathを使用して名前、会社、チャンクのサブノードを取得するか、スペースを含む子ノードをスキップします。