Edit: Updated the XML file
Requirement:
Need to read attribute name value from all the tag.
Sample XML FIle:
<ObjectConfig>
<ObjectAttribute name="A">
<ListenerRule>
<Reference name="B">
<ListenerRule>
<AttributeSource name="C">
<ApplicationRef>
<Reference name="D">
</ApplicationRef>
<RuleRef>
<Reference name="E">
</RuleRef>
</AttributeSource>
<AttributeTargets >
<AttributeTarget name="F">
<ApplicationRef>
<Reference name="G">
<ApplicationRef>
</AttributeTargets>
</ObjectAttribute>
<ObjectAttribute name="H">
<ListenerRule>
<Reference name="I">
<ListenerRule>
<AttributeSource name="J">
<ApplicationRef>
<Reference name="K">
</ApplicationRef>
<RuleRef>
<Reference name="L">
</RuleRef>
</AttributeSource>
<AttributeTargets >
<AttributeTarget name="M">
<ApplicationRef>
<Reference name="N">
<ApplicationRef>
</AttributeTargets>
</ObjectAttribute>
</ObjectConfig>
Check the code and its description below
This is my java Code. I am able to fetch the name attribute for ObjectAttribute tag. Looking to fetch name attribute for all other tags inside ObjectConfig.
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class XmlTest {
public static void main(String[] args) {
try {
File inputFile = new File("xmlPrueba.xml");
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :"+doc.getDocumentElement().getNodeName());
NodeList ObjectAttributeList = doc.getElementsByTagName("ObjectAttribute");
for (int temp = 0; temp < ObjectAttributeList.getLength(); temp++) {
Node ObjectAttributeNode = ObjectAttributeList.item(temp);
if (ObjectAttributeNode.getNodeType() == Node.ELEMENT_NODE) {
Element ObjectAttributeElement = (Element) ObjectAttributeNode;
System.out.println("Object Attribute Name : "
+ ObjectAttributeElement.getAttribute("name"));
NodeList ListenerRuleList = ObjectAttributeNode.getChildNodes();
for (int i = 0; i < ListenerRuleList.getLength(); i++) {
Node ListenerRuleNode = ListenerRuleList.item(i);
if (ListenerRuleNode.getNodeType() == Node.ELEMENT_NODE) {
NodeList ListenerReferenceRuleList = ListenerRuleNode.getChildNodes();
for(int j=0; j<ListenerReferenceRuleList.getLength(); j++){
Node ListenerReferenceRuleNode = ListenerReferenceRuleList.item(j);
if(ListenerReferenceRuleNode.getNodeType() == Node.ELEMENT_NODE){
Element ListenerReferenceRuleElement = (Element) ListenerReferenceRuleNode;
System.out.println("Listener Attribute Name : "
+ ListenerReferenceRuleElement.getAttribute("name"));
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} }
First, I put a root node around your XML document (an XML document needs a root node to be well-formed):
How the root node is named (e.g.
root
,Objects
or something else) does not matter here, it just needs to span around all other nodes.Your code tries to print the attribute
displayname
of theObject
tag.Either you wanted to print out the attribute
name
of theObject
tag or you thought you could print out theAttribute
tags with this.To print out the attribute
name
of theObject
tag, you could do something like this:To get the
name
attribute of theAttribute
tags, you'll have to callgetElementsByTagName
on theObject
element (e.g.eElement
). Then you can iterate through theNodeList
and retrieve thename
attribute of theAttribute
tags.