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
Developer
Posts: 18
Registered: ‎01-27-2010
My Device: Curve 8330
My Carrier: Verizon

Need Help Reading Xml

I have the following xml file in the following format:

<?xml version="1.0"?>
 <main>
   <letterType Value="0" type="A"/>
   <letterType Value="1" type="B"/>
   <letterType Value="2" type="C"/>
   <letterType Value="3" type="D"/>
 </main>

 

and have the following code to retrieve the value and type attributes.

//is is inputStream

Document doc = docBuilder.parse(is);

 doc.getDocumentElement().normalize();

System.out.println("Root element of doc is ") + doc.getDocumentElement().getNodeName());

NodeList listOfLetterTypes = doc.getElementsByTagName("letterType");

 

int totalElements = listOfLetterTypes.getLength();

System.out.println("Total elements: " + totalElements);

 

for(int s = 0; s < listOfLetterTypes.getLength(); s++)

{

 

   Node myNode = listOfLetterTypes.item(s);

 

   System.out.println("Node Type:"  + myNode.getNodeType());

   System.out.println("Node Name:" + myNode.getNodeName());

   Object obj = myNode.getAttributes().item(s);

 

    Attr attribute = (Attr)obj;

    attribute.normalize();

 

    System.out.println("Value: " + attribute.getOwnerElement().getAttribute("Value"));

    System.out.println("Type: " + attribute.getOwnerElement().getAttribute("type"));

}

//end of program

This is my output:

Root element of doc is main

Total elements: 4

Node type:1

letterType

Value: 0

Type: A

Node type:1

letterType

Value: 1

Type: B

Node type:1

letterType

 

For some reason the remaining types aren't printing out. I've been totally confused. Does anyone have any suggestions?

Developer
Posts: 36
Registered: ‎11-19-2008
My Device: Blackberry 9000 - Bold

Re: Need Help Reading Xml

You might want to cast myNode as an Element.  The Element object extends Node and provides more methods through which you can access attributes.

 

This code should work for you:

 

DocumentBuilderFactory docFact = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFact.newDocumentBuilder();
Document doc = docBuilder.parse(is);
doc.getDocumentElement().normalize();
System.out.println("Root element of doc is " + doc.getDocumentElement().getNodeName());
NodeList listOfLetterTypes = doc.getElementsByTagName("letterType");
int totalElements = listOfLetterTypes.getLength();
System.out.println("Total elements: " + totalElements);
for(int s = 0; s < totalElements; s++)
{
    Element myNode = (Element) listOfLetterTypes.item(s);
    System.out.println("Node Type:"  + myNode.getNodeType());
System.out.println("Node Name:" + myNode.getNodeName());
System.out.println("Value: " + myNode.getAttribute("Value")); System.out.println("Type: " + myNode.getAttribute("Type"));
}

 In this case, you are assuming that you know the element contains the Attributes of "Value" and "Type".  In the case where you do not know the type, then you will most likely have to iterate over the attributes by using myNode.getAttributes()

 

The above code generates the following output:

 

Root element of doc is main
Total elements: 4
Node Type:1
Node Name:letterType
Value: 0
Type: A
Node Type:1
Node Name:letterType
Value: 1
Type: B
Node Type:1
Node Name:letterType
Value: 2
Type: C
Node Type:1
Node Name:letterType
Value: 3
Type: D

 

 

 

 

Chirashi Security
Developer
Posts: 18
Registered: ‎01-27-2010
My Device: Curve 8330
My Carrier: Verizon

Re: Need Help Reading Xml

Thanks, for the suggestion; however my Element object "myNode" does not have the methods of getNodeName, getAttribute,  or get NodeType. This was the reason why I was placing myNode in an Object variable and casting the object variable to and an Attr object.

 

Am I doing something wrong? My workspace is configured to use the BLACKBERRY JDE Component Package 4.7.0.

Developer
Posts: 36
Registered: ‎11-19-2008
My Device: Blackberry 9000 - Bold

Re: Need Help Reading Xml

According to the 4.7.0 BlackBerry API, Element should have all the methods that I used in my code snippet.  Can you show me your import statements?  Are you importing the correct class for Element which is org.w3c.dom?

Chirashi Security
Developer
Posts: 18
Registered: ‎01-27-2010
My Device: Curve 8330
My Carrier: Verizon

Re: Need Help Reading Xml

Here are my imports:

import

i

 

import

import

import

import

 

import

import

 

import

import

import

 

import

import

import

import

import

import

import

import

import

import

import

import

import

java.io.InputStream; import javax.microedition.io.Connector; javax.microedition.io.file.*; javax.microedition.xml.rpc.Element; javax.xml.parsers.SAXParser; javax.xml.parsers.SAXParserFactory; org.w3c.dom.Attr; org.w3c.dom.Document; import org.w3c.dom.Node; org.w3c.dom.NodeList; org.xml.sax.Attributes; org.xml.sax.InputSource; net.rim.device.api.io.File; net.rim.device.api.io.FileInputStream; net.rim.device.api.ui.Field; net.rim.device.api.ui.FieldChangeListener; net.rim.device.api.ui.container.HorizontalFieldManager; net.rim.device.api.ui.container.MainScreen; net.rim.device.api.ui.component.ButtonField; net.rim.device.api.ui.component.Dialog; net.rim.device.api.ui.component.LabelField; net.rim.device.api.xml.jaxp.XMLParser; net.rim.device.api.xml.jaxp.XMLWriter; net.rim.device.api.xml.parsers.DocumentBuilder; net.rim.device.api.xml.parsers.DocumentBuilderFactory;

Developer
Posts: 36
Registered: ‎11-19-2008
My Device: Blackberry 9000 - Bold

Re: Need Help Reading Xml

Okay, you will need to change this line:

 

 

import javax.microedition.xml.rpc.Element;

 

 

to this:

 

 

import org.w3c.dom.Element;

 

 

Chirashi Security
Developer
Posts: 18
Registered: ‎01-27-2010
My Device: Curve 8330
My Carrier: Verizon

Re: Need Help Reading Xml

Thank you for showing me the error of my ways. I can't believe that it was that simple.

 

Sincerly Sheran, Thanks for help. Smiley Very Happy

Developer
Posts: 36
Registered: ‎11-19-2008
My Device: Blackberry 9000 - Bold

Re: Need Help Reading Xml

You're very welcome.  It would be helpful if you can mark this thread as solved.  

Chirashi Security