title: Parse XML File with Java
body: <p>I have an XML file that I am trying to search using Java. I need to find an element by its attribut value (Port number) and return the depending description of the element.</p>
  
  <p>The xml file of known ports should be hosted online and has an architecture like:</p>
  
  <pre><code>&lt;ports&gt;
      &lt;record&gt;
          &lt;number&gt;&lt;/number&gt;
          &lt;name&gt;&lt;/name&gt;
          &lt;protocol&gt;&lt;/protocol&gt;
          &lt;description&gt;&lt;/description&gt;
      &lt;/record&gt;
      &lt;record&gt;
          &lt;number&gt;&lt;/number&gt;
          &lt;name&gt;&lt;/name&gt;
          &lt;protocol&gt;&lt;/protocol&gt;
          &lt;description&gt;&lt;/description&gt;
      &lt;/record&gt;
      &lt;record&gt;
          &lt;number&gt;&lt;/number&gt;
          &lt;name&gt;&lt;/name&gt;
          &lt;protocol&gt;&lt;/protocol&gt;
          &lt;description&gt;&lt;/description&gt;
      &lt;/record&gt;
      &lt;record&gt;
          &lt;number&gt;&lt;/number&gt;
          &lt;name&gt;&lt;/name&gt;
          &lt;protocol&gt;&lt;/protocol&gt;
          &lt;description&gt;&lt;/description&gt;
      &lt;/record&gt;
  &lt;/ports&gt;
  </code></pre>
  
  <p>The elements have no unique identifier. In my application i want to call a function with a number as parameter and it shoult give me the description of the item with the given attribute "number". </p>
  
  <p>My problem is, its a list of all known ports and i cannot manually edit the structure to assign all elements with the portnumber as attribute. can anyone show me how to solve that?</p>
  
  <p>thanks in advance</p>
  
  <p><strong>UPDATE: i want to search it like <code>get_port(int portno)</code>. thats my code</strong>:</p>
  
  <pre><code>import java.io.File;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  public class DOMExampleJava {
  
      public static void main(String args[]) {
  
          try {
  
              File input = new File("input.xml");
              DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
              DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
              Document doc = dBuilder.parse(input);
              doc.getDocumentElement().normalize();
  
              NodeList nodes = doc.getElementsByTagName("record");
              System.out.println("==========================");
  
                  for (int i = 0; i &lt; nodes.getLength(); i++) {
                  Node node = nodes.item(i);
  
                      if (node.getNodeType() == Node.ELEMENT_NODE) {
  
                          Element element = (Element) node;
                          System.out.println("Port number: " + getValue("number", element));
                          System.out.println("Protocol: " + getValue("protocol", element));
                          System.out.println("Description: " + getValue("description", element));
  
                      }
  
                  }
  
          } catch (Exception ex) {
  
              ex.printStackTrace();
  
          }
  
      }
  
      private static String getValue(String tag, Element element) {
  
          NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
          Node node = (Node) nodes.item(0);
          return node.getNodeValue();
  
      }
  
  }
  </code></pre>