title: How to Return Tag name using attribute value inside the tag of xml using java
body: <p>I need to return the <strong>TAG Name</strong> using Java code. I have a <strong>tag name Interaction</strong> in my XML file and it had a attribute name called id. I use to give or Hard code an id value in a program. <strong>Example: String id="244_choice_list_12";</strong> I need to return Tag name. <strong>Tag Name: Interaction</strong>. Without using Xpath</p>
  
  <pre><code>package com.video.convertor;
  import java.io.File;
  import java.io.IOException;
  //import java.util.ArrayList;
  
   import javax.xml.parsers.DocumentBuilderFactory;
   import javax.xml.parsers.ParserConfigurationException;
  
   import org.w3c.dom.Document;
   import org.w3c.dom.NamedNodeMap;
   import org.w3c.dom.Node;
   import org.w3c.dom.NodeList;
   import org.xml.sax.SAXException;
  
  
  public class ReadChildId {
  
  public static void main(String[] args) 
  throws IOException, ParserConfigurationException, SAXException {
      File xmlFile = new File("/Users/myXml/Sample.xml");
      DocumentBuilderFactory   dbFactory =  DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder;
  
      dBuilder = dbFactory.newDocumentBuilder();
      Document doc = dBuilder.parse(xmlFile);
      doc.getDocumentElement().normalize();
  
      walkNode(doc);
  }
  
  private static void walkNode(Node node) {
      NodeList nodeList = node.getChildNodes();
  
      for (int i = 0; i &lt; nodeList.getLength(); i++) {
          checkChildNode(nodeList.item(i));
      }
  }
  
  private static void checkChildNode(Node node) {
      if (node.hasChildNodes()) {
          checkAttributes(node);
  
          walkNode(node);
      }
  }
  private static void checkAttributes(Node node) {
      if (node.hasAttributes()) {
          NamedNodeMap attributes = node.getAttributes();
  
          printAttributes(attributes);
      }
  }
  
  private static void printAttributes(NamedNodeMap attributes) {
  
      for (int i = 0; i &lt; attributes.getLength(); i++) {
         Node attribute = attributes.item(i);
         if (attribute.getNodeName() == "id") {
         //System.out.println("Attribute found: " +  attribute.getNodeName()+ " : " + attribute.getNodeValue());
         String x = attribute.getNodeValue();
         String attr="244_choice_list_12";
         if(attr.equals(x)){
             //Node tag = attribute.getNodeName();
              System.out.println("Tag found :"+ "\t"+ x.);
  
          }else{
              System.out.println("There No such as Tag");
          }
        /*for(int j = 0;j &lt; x.length();j++){ 
         if(attr.equals(x)){
             Node tag = attribute.getParentNode();
              System.out.println("Tag found :"+ "\t"+ tag);
  
          }else{
              System.out.println("There No such as Tag");
          }
        }*/
          //String 
         // String Value1 = attribute.getAttributes().getNamedItem("id").getNodeValue();
          //System.out.println("Attribute found: " +attribute.getNamedItem("id") + " : "+ attribute.getNodeName() + " : " + attribute.getNodeValue()));
          //System.out.println("Attribute:" +Value1 );
          }
          }
  
  }}
  </code></pre>
  
  
  
  <pre class="lang-html prettyprint-override"><code>xml File:
  &lt;itemBody class="etsmcm01fmt" id="244_item_content_3"&gt;
  &lt;rubric class="item_response_information " id="244_item_response_information_21" use="instruction" view="author proctor scorer testConstructor tutor"/&gt;
  &lt;xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="../Passage/205.qti.xml"/&gt;
  &lt;div class="stimulus_reference " id="244_stimulus_reference_4"&gt;
      &lt;p class="introduction passage-intro " id="244_introduction_5"&gt;Read&lt;span class="formatted_text text_decoration:underline " id="200244_formatted_text_6"&gt;two&lt;/span&gt; sentences&lt;/p&gt;
  &lt;/div&gt;
  &lt;Interaction class="choice_list " id="244_choice_list_12" maxChoices="4" minChoices="0" responseIdentifier="RESPONSE" shuffle="false"&gt;
      &lt;prompt id="244_item_stem_8"&gt;
          &lt;p class="stem_paragraph " id="244_stem_paragraph_9"&gt;story&lt;/p&gt;
      &lt;/prompt&gt;
      &lt;simpleChoice class="block_choice " id="200244_block_choice_13" identifier="i1"&gt;
          &lt;p class="choice_paragraph " id="200244_choice_paragraph_14"&gt;North and south&lt;/p&gt;
      &lt;/simpleChoice&gt;
      &lt;simpleChoice class="block_choice " id="200244_block_choice_15" identifier="i2"&gt;
          &lt;p class="choice_paragraph " id="200244_choice_paragraph_16"&gt;Sun and Moon&lt;/p&gt;
      &lt;/simpleChoice&gt;
      &lt;simpleChoice class="block_choice " id="200244_block_choice_17" identifier="i3"&gt;
          &lt;p class="choice_paragraph " id="200244_choice_paragraph_18"&gt;uncomfortable.&lt;/p&gt;
      &lt;/simpleChoice&gt;
      &lt;simpleChoice class="block_choice " id="200244_block_choice_19" identifier="i4"&gt;
          &lt;p class="choice_paragraph " id="200244_choice_paragraph_20"&gt;head.&lt;/p&gt;
      &lt;/simpleChoice&gt;
  &lt;/Interaction&gt;&lt;/itemBody&gt;
  </code></pre>