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: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000
Accepted Solution

cannot read nodes in xml

hi, i've got simple xml file:

 

<?xml version="1.0" encoding="UTF-8" ?> <test A="data" B="data" C="" D="" E="1"> <item type="a" max_ammount="data"/> <item type="b" max_ammount="data"/> <item type="c" max_ammount="data"/> <item type="d" max_ammount="data"/> </test>

 

but i can get only to what is in the first line i mean: A, B, C, D and E values. by such a code:

 

 

_fileConnection = (FileConnection) Connector.open("file:///store/home/user/"+_fileName, Connector.READ); _documentBuilderFactory = DocumentBuilderFactory.newInstance(); _documentBuilder = _documentBuilderFactory.newDocumentBuilder(); _is = _fileConnection.openInputStream(); _document = _documentBuilder.parse(_is); _rootElement = _document.getDocumentElement(); _rootElement.normalize(); vector.addElement(_rootElement.getAttribute("A")); //and so on

 

 

but then when i want to get to the :

<item type="a" max_ammount="data"/>
  <item type="b" max_ammount="data"/>
  <item type="c" max_ammount="data"/>
  <item type="d" max_ammount="data"/>

i cannot. i do such a thing:

 

_nodeList = _rootElement.getChildNodes();

 

for( int _i=0; _i<_nodeList.getLength(); _i++){

 

System.out.println("type" +_nodeList.item(_i).getNodeValue());

}

 

and what i get is:

type

type null

type

type null

 

and so on..

 

what is wrong? how can i get to these elements?

 

 

regards

Developer
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: cannot read nodes in xml

I think I had a similar problem before. It's either a buggy DomParser impelmentation or it requires the XML to be in a very particular syntax.

 

I ended up using the SaxParser and it works just fine. It's also more resourceful since it's not building up the whole DOM tree first. So you can create and populate your POJOs from the XML as the parser runs through it. You will need to provide a DefaultHandler implementation to the parser that is being used as a callback for events.

 

 

Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: cannot read nodes in xml

Could You provide me any simple-test example? cause i do not know how to start. i read docs, google but on the one hand-it is so many of it, on the other-i did not find anything what would describe it to me with an example so that i know how to do that. my main problem is how to get "data" from a, b, c..

Developer
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: cannot read nodes in xml

don't have an example at hand right now. But it's very straight forward. Just look through the JavaDoc of the xml packages. If you have never worked with SAX Parsers then it may be a good idea to get a brief understanding of them:

 

http://en.wikipedia.org/wiki/Simple_API_for_XML

 

Also check out the JavaDoc for DefaultHandler

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: cannot read nodes in xml

The problem, I think, is that you are retrieving the value of the <item> elements when what you want are the value of the element attributes. (The DOM model specifies that Element nodes always have a null value.) So instead of:

 

_nodeList = _rootElement.getChildNodes(); for( int _i=0; _i<_nodeList.getLength(); _i++){ System.out.println("type" +_nodeList.item(_i).getNodeValue()); }

what might work is something like:

 

_nodeList = _rootElement.getChildNodes(); for( int _i=0; _i<_nodeList.getLength(); _i++){ Node _n = _nodeList.item(_i); System.out.println("type " +_n.getAttribute("type")); System.out.println("max_ammount " + _n.getAttribute("max_ammount")); }

 

 

 

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Highlighted
Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: cannot read nodes in xml

Node _n = _nodeList.item(_i); System.out.println("type " +_n.getAttribute("type"));

 

?

getAttribute(String _s) for node? where did You find that method? this method is for Element...

yhh, i totally stucked with this problem ://

Developer
Posts: 242
Registered: ‎01-29-2009
My Device: Not Specified

Re: cannot read nodes in xml

[ Edited ]

You should be able to cast the Node to an Element. Node the is a super class of Element. Put an instanceof check around to make it safe.

 

BTW: JavaDoc can sometimes be quite useful ;-)

Message Edited by pfiffi78 on 09-04-2009 07:52 AM
Developer
Posts: 745
Registered: ‎01-04-2009
My Device: 8300, 8800, 8700f, 9000

Re: cannot read nodes in xml

ok, i've solved it:

 

for( int _i = 0; _i < _nodeList.getLength(); _i++ ){ _node = _nodeList.item(_i); if( _node.getNodeName().equals("item") ){ System.out.println.("value "+_node.getAttributes().getNamedItem("max_ammount")); } }//end of for

 

 

 

java docs are very useful. but when You do not know how to start and do not even have the general idea-You ask on the forum Smiley Happy

 

   regards and thx for help!