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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Regular Contributor
Posts: 81
Registered: ‎01-14-2011
My Device: Curve 8530
My Carrier: Cricket
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

Highlighted
Developer
Posts: 151
Registered: ‎01-20-2010
My Device: Blackberry Bold
My Carrier: Telefonica

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){    
       
}


 

pdSmiley Frustratedorry for my english.

 

 

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

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
Posts: 151
Registered: ‎01-20-2010
My Device: Blackberry Bold
My Carrier: Telefonica

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