This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes XML : getElementById returns null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML : getElementById returns null" Watch "XML : getElementById returns null" New topic
Author

XML : getElementById returns null

John Landon
Ranch Hand

Joined: Sep 25, 2008
Posts: 227
Hi,
If you look here:
api/org/w3c/dom/Document.html#getElementById(java.lang.String)
It says : "The DOM implementation must have information that says which attributes are of type ID. Attributes with the name "ID" are not of type ID unless so defined. Implementations that do not know whether attributes are of type ID or not are expected to return null."

So how do I "define so.."?

Because the only reason it returns null is because of this. My document has only 1 node with attribute ID = "the name".

Thanks.
[ December 09, 2008: Message edited by: John Landon ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

You have to create a schema, or a DTD, which declares that "id" attribute to be of ID type. (Sorry, I don't know how to do that.) And when parsing the document, you have to make sure you validate it against that schema or DTD.

If that seems like a lot of trouble you could just replace that code by a call to something that applies the equivalent XPath expression.
Matthias Wiels
Greenhorn

Joined: Dec 21, 2008
Posts: 1
At http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Attr.html#isId() you find another method.

From the use of the methods Element.setIdAttribute(), Element.setIdAttributeNS(), or Element.setIdAttributeNode()
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

That's an interesting point. I haven't seen that method before. Have you gone so far as to see if it actually solves John's problem?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: XML : getElementById returns null