java Get the list or name of all attributes in a XML element
Solution 1
Here is plain DOM based solution (however there is nothing wrong to combine XPath with DOM in Java):
NodeList baseElmntLst_gold = goldAnalysis.getElementsByTagName("base");
Element baseElmnt_gold = (Element) baseElmntLst_gold.item(0);
NamedNodeMap baseElmnt_gold_attr = baseElmnt_gold.getAttributes();
for (int i = 0; i < baseElmnt_gold_attr.getLength(); ++i)
{
Node attr = baseElmnt_gold_attr.item(i);
System.out.println(attr.getNodeName() + " = \"" + attr.getNodeValue() + "\"");
}
NodeList innerElmntLst_gold = baseElmnt_gold.getChildNodes();
Element innerElement_gold = null;
for (int i = 0; i < innerElmntLst_gold.getLength(); ++i)
{
if (innerElmntLst_gold.item(i) instanceof Element)
{
innerElement_gold = (Element) innerElmntLst_gold.item(i);
break; // just get first child
}
}
NamedNodeMap innerElmnt_gold_attr = innerElement_gold.getAttributes();
for (int i = 0; i < innerElmnt_gold_attr.getLength(); ++i)
{
Node attr = innerElmnt_gold_attr.item(i);
System.out.println(attr.getNodeName() + " = \"" + attr.getNodeValue() + "\"");
}
Result:
baseAtt1 = "aaa"
baseAtt2 = "tt"
att1 = "one"
att2 = "two"
att3 = "bazinga"
Solution 2
If you use XPath you will have less code, but for a dom base solution I have a suggestion here:
public void printElementsAndAttributes() throws Exception {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document doc = db.parse(new File("test.xml"));
NodeList base = doc.getElementsByTagName("base");
Node basenode = base.item(0);
System.out.println(basenode.getNodeName() + getAttributesAsString(basenode.getAttributes()));
NodeList children = basenode.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node item = children.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(item.getNodeName() + getAttributesAsString(item.getAttributes()));
}
}
}
private String getAttributesAsString(NamedNodeMap attributes) {
StringBuilder sb = new StringBuilder("\n");
for (int j = 0; j < attributes.getLength(); j++) {
sb.append("\t- ").append(attributes.item(j).getNodeName()).append(": ").append(attributes.item(j).getNodeValue()).append("\n");
}
return sb.toString();
}
Solution 3
You can use this XPath to retrieve all attributes of 1st element
node:
base/element[1]/@*
To get all attributes of all nodes in your XML yo can use this expression:
//@*
Solution 4
Use this method..
public static void listAllAttributes(Element element) {
System.out.println("List attributes for node: " + element.getNodeName());
// get a map containing the attributes of this node
NamedNodeMap attributes = element.getAttributes();
// get the number of nodes in this map
int numAttrs = attributes.getLength();
for (int i = 0; i < numAttrs; i++) {
Attr attr = (Attr) attributes.item(i);
String attrName = attr.getNodeName();
String attrValue = attr.getNodeValue();
System.out.println("Found attribute: " + attrName + " with value: " + attrValue);
}
}
call this method by using following call in the main method
NodeList entries = doc.getElementsByTagName("NameOfTheNode");
int num = entries.getLength();
for (int i=0; i<num; i++) {
Element node = (Element) entries.item(i);
listAllAttributes(node);
}
Comments
-
yossi about 4 years
i have an xml element
<base baseAtt1="aaa" baseAtt2="tt"> <innerElement att1="one" att2="two" att3="bazinga"/> </base>
and i would like to get the list of attributes. for both the base element and the inner element.
i dont know the name of the innerElement it can have many different names.
NodeList baseElmntLst_gold = goldAnalysis.getElementsByTagName("base"); Element baseElmnt_gold = (Element) baseElmntLst_gold.item(0);
the goal is to get a kind of dictionary as output,
for example for the xml above the output will be a dictionary with those valuse.
baseAtt1 = "aaa" baseAtt2 = "tt" att1 = "one" att2 = "two" att3 = "bazinga"
i am using jre 1.5