To select all attributes of all elements of a document Element1: //Element1/@* . This will return a node node containing attribute nodes. Then you can iterate over the node nodes.
If you already have a node context and want to find the results under it, the request will be .//Element1/@* . This is usually more efficient than querying the entire document.
// input is an InputSource or a DOM node NodeList nl = (NodeList) xpath.evaluate("//Element1/@*", input, XPathConstants.NODESET); int length = nl.getLength(); for( int i=0; i<length; i++) { Attr attr = (Attr) nl.item(i); String name = attr.getName(); String value = attr.getValue(); }
And it may be more efficient to find all the elements of a given name using getElementsByTagName .
NodeList nl = document.getElementsByTagName("Element1");
To get the attributes of a specific element, iterate over its attribute properties.
NamedNodeMap nl = element.getAttributes(); int length = nl.getLength(); for( int i=0; i<length; i++) { Attr attr = (Attr) nl.item(i); String name = attr.getName(); String value = attr.getValue(); }
Lachlan roche
source share