I'm at a dead end, hopefully I just did a dumb thing that I can easily fix.
I pass a string full of XML, being "XMLstring". I want to get one of the elements and print the child nodes in "name = value" on the console. The problem is that the console continues to print garbage along with the name of the item that I cannot figure out how to get rid of.
In any case, this code:
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(XMLstring));
Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("client-details");
Node node = nodes.item(0);
NodeList client_details = node.getChildNodes();
for (int i = 0; i < client_details.getLength(); i++) {
System.out.println(client_details.item(i).getNodeName()+" = "+getTextContents(client_details.item(i)));
}
}
catch (Exception e) {
e.printStackTrace();
}
Gives me the following:
testing-mode = false
name = testman
age = 30
Why is he typing "#text ="? How can I get rid of it?
I use NetBeans if this helps.
source
share