Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Contributor
Posts: 17
Registered: ‎06-01-2012
My Device: 8520
My Carrier: Ufone

Processing Nested XML Nodes Document for Blackberry JRE 5.0?

[ Edited ]

Hi All,

 

I am trying to read an XML document. The structure of XML is given below,

 

 <?xml version="1.0" encoding="utf-8" standalone="yes"?>
 <Home xmlVersion="1.0" name="Videos" orderNo="5" subText="Hosted at YouTube" introLink="http://www.youtube.com">
 <Level1 name="Introduction" orderNo="1" subText="About Us">
 <Level2 name="About Us" orderNo="1" videoLink="http://www.youtube.com" />
 </Level1>
 <Level1 name="How To" orderNo="2" subText="How toVideos" >
 <Level2 name="More 1" orderNo="1videoLink="http://www.youtube.com" />
 <Level2 name="More 2" orderNo="2videoLink="http://www.youtube.com" />
 <Level2 name="More 3" orderNo="3" videoLink="http://www.youtube.com" />
 <Level2 name="More 4" orderNo="4" videoLink="http://www.youtube.com" />
 <Level2 name="More 5" orderNo="5" videoLink="http://www.youtube.com" />
 <Level2 name="More 6" orderNo="6" videoLink="http://www.youtube.com" />
 <Level2 name="More 7" orderNo="7" videoLink="http://www.youtube.com" />
 <Level2 name="More 8" orderNo="8" videoLink="http://www.youtube.com" />
 <Level2 name="More 9" orderNo="9videoLink="http://www.youtube.com" />
 </Level1>
 

</Home>


 

I want the output in the following format,

 

Introduction (As heading)

About US : http://www.youtube.com(As Menu Item)

 

How To (As heading)

More 1 : http://www.youtube.com(As Menu Item)

More 2 : http://www.youtube.com(As Menu Item)

More 3 : http://www.youtube.com(As Menu Item)

More 4 : http://www.youtube.com(As Menu Item)

More 5 : http://www.youtube.com(As Menu Item)

More 6 : http://www.youtube.com(As Menu Item)

More 7 : http://www.youtube.com(As Menu Item)

More 8 : http://www.youtube.com(As Menu Item)

More 9 : http://www.youtube.com(As Menu Item)

 

 

I am using the following code but it can handle only one first node.

 

HttpConnection http = (HttpConnection)Connector.open(url);
String tmpNodeVal = "";

// Build a document based on the XML file.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream inputStream = http.openInputStream();
Document document = builder.parse( inputStream );

inputStream.close();
http.close();
// Normalize the root element of the XML document. This ensures that all Text
// nodes under the root node are put into a "normal" form, which means that
// there are neither adjacent Text nodes nor empty Text nodes in the document.
// See Node.normalize().
Element rootElement = document.getDocumentElement();
rootElement.normalize();

NodeList list = document.getElementsByTagName("error");
list.getLength();

NodeList nl = document.getChildNodes();
if (nl == null)
{
Dialog.alert("NodeList Null");
}
else
{
for (int i = 0; i < nl.getLength(); i++){
Node first = nl.item(i);
if (first.getNodeType() == Node.ELEMENT_NODE){
System.out.println("first:" + first);
NamedNodeMap att = first.getAttributes();
Dialog.alert("Attributes Length : " +att.getLength());
for (int a = 0; a < att.getLength() - 1; a++)
{
Node theAttribute = att.item(a);
tmpNodeVal = theAttribute.getNodeName() +" : " +theAttribute.getNodeValue();
}
}
}
}
return tmpNodeVal;

 

 

 

Any help would be highly appreciated.

 

Regards