Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
ahisaye
Posts: 73
Registered: 05-27-2009
Accepted Solution

XML Parsing

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?

 

Please use plain text.
Developer
simon_hain
Posts: 10,780
Registered: 07-29-2008
My Carrier: O2 Germany

Re: XML Parsing

yes
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.

peter_strange wrote:
"This process should happen traumatically for you in both JDE and Eclipse."
Please use plain text.
Developer
Ted_Hopp
Posts: 1,287
Registered: 01-21-2009

Re: XML Parsing

That list.length (list.getLength()?) is 1 isn't surprising or particularly useful; it just means that the HTML doesn't contain any "node1" tags. The list that's interesting (and where you should be looking for the source of the problem) is list.item(0).getChildNodes(). Check the node type of everything in that list, and don't assume that getFirstChild() is what you're after.



Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
ahisaye
Posts: 73
Registered: 05-27-2009

Re: XML Parsing

[ Edited ]

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

Message Edited by ahisaye on 08-28-2009 04:00 PM
Please use plain text.
Developer
Ted_Hopp
Posts: 1,287
Registered: 01-21-2009

Re: XML Parsing

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 + ")";
}
}

 


 

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
ahisaye
Posts: 73
Registered: 05-27-2009

Re: XML Parsing

Sorry I meant it printed node1.

 

That returns:

1 children:
 [0]: CDATA section named "#cdata-section"

Please use plain text.
Developer
ahisaye
Posts: 73
Registered: 05-27-2009

Re: XML Parsing

Ugh.. I removed the dbf1.setIgnoringComments(false); to true and it works.. thanks
Please use plain text.
New Developer
bala_eventurers
Posts: 6
Registered: 11-04-2009

Re: XML Parsing

can anyone help me for getting xml parser in BB

Please use plain text.