Element getElementById ( String name ) { for ( Attr attr : attributes ) { if ( attr . isId ( ) && name . equals ( attr . getValue ( ) ) ) { return this ; } } /* * TODO: Remove this behavior. * The spec explicitly says that this is a bad idea. From * Document.getElementById(): "Attributes with the name "ID" * or "id" are not of type ID unless so defined. */ if ( name . equals ( getAttribute ( "id" ) ) ) { return this ; } for ( NodeImpl node : children ) { if ( node . getNodeType ( ) == Node . ELEMENT_NODE ) { Element element = ( ( ElementImpl ) node ) . getElementById ( name ) ; if ( element != null ) { return element ; } } } return null ; }
This implementation walks the entire document looking for an element with the given ID attribute . We should consider adding an index to speed navigation of large documents .