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
Regular Contributor
zumwaltwood
Posts: 81
Registered: ‎01-14-2011
My Device: Curve 8530
Accepted Solution

xml.. nodes.. and elements.. oh my...

[ Edited ]

The data that I have coming back from my web service is in the following structure

<DocumentElement>
  <SearchResults>
    <CID>74</CID>
    <CME>Testmark Corporation</CME>
  </SearchResults>
  <SearchResults>
    <CID>739</CID>
    <CME>Testing Services</CME>
  </SearchResults>
  <SearchResults>
    <CID>753</CID>
    <CME>E. Tester</CME>
  </SearchResults>
  <SearchResults>
    <CID>292</CID>
    <CME>Test Name 1</CME>
  </SearchResults>
</DocumentElement>

 

Now. all I want to do is to translate this data, so first because I can't use a string into a stream since that is all the parse for the document uses, I save the file to the phone, no big deal, double work, what ever, now on to the problem, when trying to parse this list I have the following code:

 

try
{
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  InputStream inputStream = (InputStream)Connector.openInputStream(_xmlFileName);
  Document document = builder.parse( inputStream );
  Element rootElement = document.getDocumentElement();
  rootElement.normalize();			            
  NodeList list=rootElement.getElementsByTagName("SearchResults");
  if(list.getLength()>0)
  {
    results=new KeyValuePair[list.getLength()];
    for(int i=0;i<list.getLength();i++)
    {
        Node node = (Node)list.item(i);
	if(node!=null)
	{
	    NodeList childNodes = node.getChildNodes();
	    if(childNodes.getLength()>0)
	    {
	        Node CID = childNodes.item(1); // for some reason this is always 1 and not 0
		Node CME = childNodes.item(2); // this does not exist no matter what I try
		String cid = (CID.getNodeValue()==null)?CID.getNodeName():CID.getNodeValue();
		String cname = (CME.getNodeValue()==null)?CME.getNodeName():CME.getNodeValue();
		KeyValuePair kp = new KeyValuePair(cid, cname);
		log("---------------------------------------");
		log("CID: " + cid);
		log("CME: " + cname);
		log("---------------------------------------");
		results[i]=kp;
	    }
        }
    }
    log("Total Results: " + list.getLength());
    log("Items to list: " + results.length);
  }			
}
catch(Exception ex)
{
  log(ex.toString());
}

 

Now, from what have discovered is that the node name is coming through ONLY for CID, not for CME, and I can't get the node value, this code for the child nodes should only have a count for 2 for the child nodes, but it has the same count as the parent which is 4 which is how many are in the file, what on earth am I over looking?

BlackBerry Java JDK 6.0.0.30
BlackBerry Java Plug-in 1.3.0.201101051005-15
BlackBerry JRE 6.0.0
Eclipse: Helios

Developer
carlostheone
Posts: 151
Registered: ‎01-20-2010
My Device: Blackberry Bold

Re: xml.. nodes.. and elements.. oh my...

You can try something like this:

 

	
String _node,_element,_node2,_element2
DocumentBuilderFactory docBuilderFactory= DocumentBuilderFactory. newInstance(); 
DocumentBuilder docBuilder= docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();
Document doc = docBuilder.parse(conn.openInputStream());
doc.getDocumentElement ().normalize ();
NodeList list=doc.getElementsByTagName("CID");
       NodeList list2=doc.getElementsByTagName("CME");
       for (int i=0;i<list.getLength();i++){
           Node value=list.item(i).getChildNodes().item(0);
           Node value2=list2.item(i).getChildNodes().item(0);
           
           _node=list.item(i).getNodeName();
           _element=value.getNodeValue();
           
   _node2=list2.item(i).getNodeName();
           _element2=value2.getNodeValue();
        KeyValuePair kp = new KeyValuePair(_element, _element2);
   ........
          
           
       }
}catch (Exception e){    
       
}


 

pd:smileyfrustrated:orry for my english.

 

 

Regular Contributor
zumwaltwood
Posts: 81
Registered: ‎01-14-2011
My Device: Curve 8530

Re: xml.. nodes.. and elements.. oh my...

Thanks a ton, don't think I would have ever gone that route alone, works perfectly.

 

BlackBerry Java JDK 6.0.0.30
BlackBerry Java Plug-in 1.3.0.201101051005-15
BlackBerry JRE 6.0.0
Eclipse: Helios

Developer
carlostheone
Posts: 151
Registered: ‎01-20-2010
My Device: Blackberry Bold

Re: xml.. nodes.. and elements.. oh my...

Dont worry, I had the same problem 3 months ago.

And good luck with your app.

 

pd: Sorry for my english