0
我試着用DOM解析下面的XML。我無法打印元素的值。任何人都可以幫我打印元素的值嗎?提前使用DOM解析XML並在java中打印
感謝
<tns:CustomerDetails xmlns:xe="http://www.w3.org/2001/04/xmlenc#" xmlns:xd="http://www.w3.org/2000/09/xmldsig#" xmlns:tns="http://abc.com/elements" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pf="http://abc.com/Service">
<tns:CustomerId>360817</tns:CustomerId>
<tns:CustomerName>ABC Corp</tns:CustomerName>
<tns:PAN>awap</tns:PAN>
<tns:Timestamp>2010-05-20T12:20:19Z</tns:Timestamp>
<tns:RequestId>397</tns:RequestId>
<tns:PIN>1234</tns:PIN>
</tns:CustomerDetails>
我的代碼
File infile = new File("D:\\Cust.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(infile);
doc.getDocumentElement().normalize();
System.out.println("root of xml file " +
doc.getDocumentElement().getNodeName());
System.out.println("==========================");
NodeList list = doc.getElementsByTagName("CustomerDetails");
System.out.println(list.getLength());
Element name = doc.getElementById("CustomerId");
if (name == null) {
System.out.println("There is no element with the ID ");
} else {
Text text = (Text)name.getFirstChild();
System.out.println("The ID " + " locates the name " + text.getData());
}
我試圖與
Element name = doc.getElementById("tns:CustomerId");
也.. 我得到空當打印
爲什麼不告訴你,你試過嗎? – home
提示,Element類和所有其他Node類沒有有意義的'toString()'方法。 – jtahlborn
發表您寫的代碼。 – Raedwald