Java: XML getElementById returns NULL
June 11, 2010 23:11:59 Last update: June 11, 2010 23:14:02
Given a simple XML file like this:
Calling
In fact the JavaDoc says something along the lines that
How is an attribute defined as an
So, what to do if you want to find an element for which the attribute named "id" has a given value? Several options were offered in GetElementById Pitfalls. One of them is to use an XPath expression like this:
Also, in the Apache XML Security project, the class
Another reference:
Java: How to make getElementById() work using xml schema
<?xml version="1.0"?> <root id="1"> <node id="12"> <text id="123">Some garbage.</text> </node> </root>
Calling
Document.getElementById returns null (surprisingly!):
import java.io.*; import org.w3c.dom.*; import javax.xml.parsers.*; public class TestGetElementById { public static void main(String[] args) throws Exception { String id = "123"; if (args.length > 0) { id = args[0]; } DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); System.out.println("Is Validating: " + domFactory.isValidating()); domFactory.setNamespaceAware(true); DocumentBuilder docBuilder = domFactory.newDocumentBuilder(); Document doc = docBuilder.parse("simple.xml"); Node n = doc.getElementById(id); if (n == null) { System.out.println("Failed to find node for id: " + id); } else { System.out.println("Found node: " + n); } } }
In fact the JavaDoc says something along the lines that
getElementById returns the Element that has an ID attribute with the given value. An attribute with the name "ID" or "id" is not of type ID unless it is so defined.
How is an attribute defined as an
ID attribute? With a DTD or schema. If you are not validating the XML, then the API is useless.
So, what to do if you want to find an element for which the attribute named "id" has a given value? Several options were offered in GetElementById Pitfalls. One of them is to use an XPath expression like this:
"//*[@id = 'myId']".
Also, in the Apache XML Security project, the class
org.apache.xml.security.utils.IdResolver was specifically created to address this problem. Calling IdResolver.getElementById(doc, id) will return the element you are looking for.
Another reference:
Java: How to make getElementById() work using xml schema