If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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
bbman
Posts: 16
Registered: ‎06-01-2009
My Device: Not Specified
Accepted Solution

To get node attributes of a node using a DOM parser

Hi, is there an easier way to get the attributes of a node using a DOM parser. I don't understand the method getAttributes() since it returns a NamedNodeMap where all the getters are returning a Node again. I just have this simple xml structure:

<TYPE id="1">editfield</TYPE>
<QUESTION id="1">First Name</QUESTION>
<ANSWER id=""/>

 

For example, I am able to get "editfield" and "First Name" but I want to be able to get the id attribute of the corresponding nodes as well. The getNodeValue() just returns me null, and getLocalName() and getNodeName() just returns to me TYPE or QUESTION.

 

Please any help if anyone knows. Oh and I do want to use DOM parser, not SAX. Thank you very much in advance

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: To get node attributes of a node using a DOM parser

[ Edited ]
Try something like this:

 

if (node.getNodeType() == Node.ENTITY_NODE) {
Entity entity = (Entity) node;
String id = entity.getAttribute("id");
// etc.
}

 

 Obviously, if you already know that the node is an entity, you don't need the "if" test.

 

P.S. The Node objects that are accessible from a NamedNodeMap are all Attr nodes (getNodeType() == ATTRIBUTE_NODE). When you look at the Attr interface, it has several useful methods. You can even check whether an attribute is present because it was in the XML document or (in the case of documents with a DTD) it was generated as a default attribute value.

Message Edited by Ted_Hopp on 06-16-2009 08:31 PM



Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
Posts: 432
Registered: ‎02-23-2009
My Device: Not Specified

Re: To get node attributes of a node using a DOM parser

Try SAXparser instead of DOM parser to retrieve attributes of a node

 


Press the kudos button to thank the user who helped you.

If your problem was get solved then please mark the thread as "Accepted solution".

 

 

 

Deepesh Gupta
Please use plain text.
Developer
bbman
Posts: 16
Registered: ‎06-01-2009
My Device: Not Specified

Re: To get node attributes of a node using a DOM parser

In reply to Ted,

 

node.getNodeType() == Node.ENTITY_NODE is fals, but I can get node.getNodeName() and it is "TYPE". Well, I skipped the if anyway and I can't cast the node to an entity, it gave a cast exception. However the thing is, even if I can get the node as an entity, the entity class doesn't have a getAttribute(String s), it only has getAttributes() and again it returns a NodeNamedMap.

 

Argh, I am forced to switch to make a SAX parser instead, aren't I? :smileysad:  There goes my 2 days of development

Please use plain text.
Developer
bbman
Posts: 16
Registered: ‎06-01-2009
My Device: Not Specified

Re: To get node attributes of a node using a DOM parser

I found out how to get the attributes after some googling and modifications. Here it is

 

                NamedNodeMap attributes = (NamedNodeMap)tempNode.getAttributes();
                for (int g = 0; g < attributes.getLength(); g++) {
                    Attr attribute = (Attr)attributes.item(g);
                    System.out.println(" Attribute: " + attribute.getName() +
                    " with value " +attribute.getValue());
                }

 

So you do have to use NamedNodeMap, I just didn't know how to before. You have to do the casting trick too. Great, I don't have to use SAX after all now

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: To get node attributes of a node using a DOM parser

There was a typo in my code. The test should be node.getNodeType() == Node.ELEMENT_NODE. With that, it should work. That's what I get for posting code when I'm tired.



Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Contributor
jalpapunjabi
Posts: 12
Registered: ‎11-03-2010
My Device: Not Specified

Re: To get node attributes of a node using a DOM parser

Hey bbman,

 

Thanks so much. Your solution has been really helpful to me. Saved me a lot of time and effort.

Please use plain text.