For the getElementById()
call to work, the Document
has to know the types of its nodes, and the target node must be of the XML ID type for the method to find it. It knows about the types of its elements via an associated schema. If the schema is not set, or does not declare the id
attribute to be of the XML ID type, getElementById()
will never find it.
My guess is that your document doesn't know the p
element's id
attribute is of the XML ID type (is it?). You can navigate to the node in the DOM using getChildNodes()
and other DOM-traversal functions, and try calling Attr.isId()
on the id attribute to tell for sure.
From the getElementById javadoc:
The DOM implementation is expected to
use the attribute Attr.isId to
determine if an attribute is of type
ID.
Note: Attributes with the name "ID" or
"id" are not of type ID unless so
defined.
If you are using a DocumentBuilder
to parse your XML into a DOM, be sure to call setSchema(schema)
on the DocumentBuilderFactory before calling newDocumentBuilder(), to ensure that the builder you get from the factory is aware of element types.