title: XPath Select Attribute Values from Parent AND Child Nodes
body: <p>Following is my XML File:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
     &lt;query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:lang="en-GB"&gt;
        &lt;results&gt;
  
          &lt;sector sectorid="1" sectorname="Basic Materials"&gt;
            &lt;industry id="112" name="Agricultural Chemicals"/&gt;
            &lt;industry id="132" name="Aluminum"/&gt;
            &lt;industry id="110" name="Chemicals - Major Diversified"/&gt;
            &lt;industry id="131" name="Copper"/&gt;
            &lt;industry id="134" name="Gold"/&gt;
            &lt;industry id="121" name="Independent Oil and Gas"/&gt;
            &lt;industry id="120" name="Major Integrated Oil and Gas"/&gt;
          &lt;/sector&gt;
  
          &lt;sector sectorid="2" sectorname="Conglomerates"&gt;
            &lt;industry id="210" name="Conglomerates"/&gt;
          &lt;/sector&gt;
  
          &lt;sector sectorid="7" sectorname="Services"&gt;
            &lt;industry id="720" name="Advertising Agencies"/&gt;
            &lt;industry id="773" name="Air Delivery and Freight Services"/&gt;
            &lt;industry id="772" name="Air Services and Others"/&gt;
            &lt;industry id="730" name="Apparel Stores"/&gt;
            &lt;industry id="744" name="Auto Dealerships"/&gt;
          &lt;/sector&gt;
  
       &lt;/results&gt;
     &lt;/query&gt;
  </code></pre>
  
  <p>From the above XML file, I am looking to store the values of attributes: <code>sectorid</code>, <code>id</code>, and <code>name</code> in the appropriate variables (I'm using Java). I have been looking at different XPath expressions and I came up with the following code, however, a <code>java.lang.NumberFormatException: For input string: ""</code> exception is thrown when storing the value of <code>id</code> attribute. Here's my code:</p>
  
  <pre><code>public class XMLToDatabase {
  
      private static int __SectorID;
      private static int __IndustryID;
      private static String __IndustryName;
  
      public static void main(String[] args) throws SQLException, UnsupportedEncodingException, ParserConfigurationException, SAXException, IOException, XPathExpressionException {
  
          try {               
              File _XMLFile = new File("SectorsAndIndustries.xml");
  
              DocumentBuilderFactory _DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
              _DocumentBuilderFactory.setNamespaceAware(true);
  
              DocumentBuilder _DocumentBuilder = _DocumentBuilderFactory.newDocumentBuilder();
              Document _Document = _DocumentBuilder.parse(_XMLFile);  
  
              _Document.getDocumentElement().normalize();
  
              XPath _XPath = XPathFactory.newInstance().newXPath();
  
              XPathExpression _XPathExpression = _XPath.compile("//sector | //industry");
  
              NodeList _NodeList = (NodeList) _XPathExpression.evaluate(_Document, XPathConstants.NODESET);
  
  
              for (int i = 0; i &lt; _NodeList.getLength(); i++) {
                  Node _Node = _NodeList.item(i);
  
                  if(_Node.getNodeType() == Node.ELEMENT_NODE) {
                      Element _Element = (Element) _Node;
  
                      __SectorID = Integer.parseInt(_Element.getAttribute("sectorid"));
                      __IndustryID = Integer.parseInt(_Element.getAttribute("id"));
                      __IndustryName = _Element.getAttribute("name");
                  }
  
              System.out.println(__SectorID + ", " + __IndustryID + ", " + __IndustryName);
              }
          } catch (Exception e) {
               e.printStackTrace();
          }
  
      }
  
  }
  </code></pre>
  
  <p>Could someone please help me determine if it is the <code>XPath Expression</code> that I am making a mistake with, or if is the way I am storing the second variable <code>__IndustryID</code>? Because the first variable <code>__SectorID</code> correctly stores the value <code>1</code>, but throws the above mentioned exception for <code>__IndustryID</code>. Ideally, I would want to store the values of all 3 attributes every time the <code>for</code> loop is executed, to save them to a database table. Please let me know if any more information is required.</p>