<persons>
<people id="1">
<name>Kumar</name>
<address>
<street doorNumber="1a">First Cross Street</street>
<city>Chennai</city>
<state>TamilNadu</state>
<country>India</country>
<postcode>600001</postcode>
</address>
<Company id="C101">TCS</Company>
<sex>Male</sex>
</people>
</persons>
我想使用 dom 以这种方式获得输出。人员 ID:1 姓名:Kumar 地址:街道:城市:州:国家:邮政编码:公司 ID 公司:性别:
我无法访问街道、城市,即所有子节点及其属性。这是我的代码。
NodeList nList = doc.getElementsByTagName("people");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("People ID:" +eElement.getAttribute("id"));
System.out.println("Name:" +eElement.getElementsByTagName("name").item(0).getTextContent());
System.out.println("Address:" +eElement.getElementsByTagName("address").item(0).getTextContent());
System.out.print("Sex:" +eElement.getElementsByTagName("sex").item(0).getTextContent());
}