08-27-2009 07:46 PM
I get an XML from a URL and try to parse it, but its not working. The xml is like this:
<node>
<node1>
HTML here
</node1>
</node>
My code is as follows:
DocumentBuilderFactory dbf1 = DocumentBuilderFactory.newInstance(); dbf1.setValidating(false); dbf1.setExpandEntityReferences(true); dbf1.setIgnoringComments(true); dbf1.setNamespaceAware(false); db = dbf1.newDocumentBuilder(); try { doc = db.parse(input); } catch (SAXException e) {} NodeList list = (NodeList) doc.getElementsByTagName("node1"); System.out.println("VALUE:" + list.item(0).getFirstChild().getTextContent());
The System.out.println prints VALUE: null, while printing the node name prints the correct value, and list.length is 1.
What is wrong? Could the HTML tags be messing it up?
Solved! Go to Solution.
08-28-2009 01:58 AM
08-28-2009 11:54 AM
08-28-2009 02:51 PM - last edited on 08-28-2009 04:00 PM
If I print out the node name its correct (content)
but the value is always null.
System.out.println("VALUE:" + list.item(0).getTextContent());Also returns null
08-28-2009 04:21 PM
I don't quite understand that. Are you saying that the return value from getNodeName() is the string "content"? That's weird if you have HTML under node1, because there is no such tag. Try something like this and post the output here:
// ...NodeList list = (NodeList) doc.getElementsByTagName("node1");
Node html = list.item(0);
NodeList children = node.getChildNodes();
int n = children.getLength();
System.out.println(n + " children:");
for (int i = 0; i < n; ++i) {
Node node = children.item(i);
System.out.println(" [" + i + "]: " + nodeTypeName(node.getNodeType())+ " named \"" + node.getNodeName() + "\""
);
}
// ...
}
private static String nodeTypeName(short type) {
switch (type) {
case Node.ATTRIBUTE_NODE: return "Attribute";
case Node.CDATA_SECTION_NODE: return "CDATA section";
case Node.COMMENT_NODE: return "Comment";
case Node.DOCUMENT_NODE: return "Document";
case Node.DOCUMENT_FRAGMENT_NODE: return "Document fragment";
case Node.DOCUMENT_TYPE_NODE: return "Document type";
case Node.ELEMENT_NODE: return "Element";
case Node.ENTITY_NODE: return "Entity";
case Node.ENTITY_REFERENCE_NODE: return "Entity reference";
case Node.NOTATION_NODE: return "Notation";
case Node.PROCESSING_INSTRUCTION_NODE: return "Processing instruction";
case Node.TEXT_NODE: return "Text";
default: return "Unknown node type (" + type + ")";
}
}
08-28-2009 04:46 PM
Sorry I meant it printed node1.
That returns:
1 children:
[0]: CDATA section named "#cdata-section"
08-28-2009 04:50 PM
11-04-2009 08:54 AM
can anyone help me for getting xml parser in BB