How to retrieve the attribute values from the tags in XML File

108 Views Asked by At

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();
  }

} }

Output: enter image description here

1

There are 1 best solutions below

0
On

First, I put a root node around your XML document (an XML document needs a root node to be well-formed):

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <Object name="a"/>
    <Object name="b"/>
    <Object name="c">
        <Attribute name="atnameC"/>
    </Object>
    <Object name="d">
        <Attribute name="atnameD"/>
    </Object>
</root>

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 the Object tag.
Either you wanted to print out the attribute name of the Object tag or you thought you could print out the Attribute tags with this.

To print out the attribute name of the Object tag, you could do something like this:

System.out.println("Object name: " + objectElement.getAttribute("name"));

To get the name attribute of the Attribute tags, you'll have to call getElementsByTagName on the Object element (e.g. eElement). Then you can iterate through the NodeList and retrieve the name attribute of the Attribute tags.

NodeList attributeNodeList = eElement.getElementsByTagName("Attribute");
for (int attrNum = 0; attrNum < attributeNodeList.getLength(); attrNum++) {
    Node attributeNode = attributeNodeList.item(attrNum);
    System.out.println("Object " + eElement.getAttribute("name") + " has Attribute: "
            + attributeNode.getAttributes().getNamedItem("name").getNodeValue());
}