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
dmglakewood
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified
Accepted Solution

XML how to select multiple nodes

I have an xml document I'm pulling from a website url. The XML contains a lot of stuff I do not need. I would just like to select a few pieces of the XML and display them. I'm using this to pull from 1 node

 

NodeList list = doc.getElementsByTagName("NODE_NAME");

 

It works great, but it will only pull from 1 node. Does anyone know how to pull from ultiple nodes at the same time?

Developer
JCarty
Posts: 1,055
Registered: ‎01-25-2009
My Device: BlackBerry Bold 9650/BlackBerry Torch 9800

Re: XML how to select multiple nodes

The list that is returned from getElementsByTagName should return all nodes with that tag name. Are you trying to get attributes from those nodes?

 

If you just need all nodes by that tag name (i.e. <Address>), just loop through the items in the list returned by that method.

 

If you need an example, let me know.

Jerome Carty - Follow jcarty on Twitter@jcarty | #webworks-dev / #BlackberryDev on irc.freenode.net | My Apps
Click "Accept as Solution" if post solved your original issue.. Give like/thumbs up if you feel post is helpful
Developer
dmglakewood
Posts: 296
Registered: ‎12-07-2008
My Device: Not Specified

Re: XML how to select multiple nodes

Sorry I explained it wrong.

 

The xml is setup like so

 

<main cat>

     <something1></something1>

     <something2></something2>

</main cat>

 

How would I be able to get what's in something1 and something2?

 

It's hard to explain but I hope that helps a little

Developer
JCarty
Posts: 1,055
Registered: ‎01-25-2009
My Device: BlackBerry Bold 9650/BlackBerry Torch 9800

Re: XML how to select multiple nodes

[ Edited ]

The text inside of something1 and something2 are also nodes.

 

Once you have the node from your nodelist. you are going to have to loop through childNodes().

 

This is the code i've used below. Its best used for one unique tag but I've turned it into a method so I can retrieve the text from any node:

 

String ret = null;

 

NodeList _nodelist = doc.getElementsByTagName("something1");
       
        if(_nodelist.getLength() == 1) {
            Node n = _nodelist.item(0);


            if(n != null) {
                NodeList nl2 = n.getChildNodes();
                Node n2 = nl2.item(0);
               
                ret = n2.getNodeValue(); // Your inner text
            }
        }

 

Let me know if this works out for you.

Jerome Carty - Follow jcarty on Twitter@jcarty | #webworks-dev / #BlackberryDev on irc.freenode.net | My Apps
Click "Accept as Solution" if post solved your original issue.. Give like/thumbs up if you feel post is helpful