title: How to use the SAX parser to read XML tags contained within other tags
body: <p>This is my first post so if I haven't made myself clear I'll happily provide more details.
  I'm writing a routing API using Java. The majority of the file is parsing correctly bar one part. The part of the XML I'm interested in looks like this:</p>
  
  <pre><code>&lt;way id="30184957" user="Central America" uid="69853" visible="true" version="2" changeset="4961491" timestamp="2010-06-11T10:52:19Z"&gt;
    &lt;nd ref="332597303"/&gt;
    &lt;nd ref="332597551"/&gt;
    &lt;nd ref="332597552"/&gt;
    &lt;tag k="highway" v="residential"/&gt;
    &lt;tag k="name" v="Rae's Court"/&gt;
  &lt;/way&gt;
  &lt;/b&gt;
  </code></pre>
  
  <p>The relevant part of my code looks like this:</p>
  
  <pre><code>public void startElement(String uri, String localName, String qName, Attributes attributes) 
  {
      if (qName == "node") //if the tag value is node
      {   
          Node currentNode = new Node(0, 0, 0); //new node with all 0 values
          currentNode.nodeID = Integer.parseInt(attributes.getValue(0)); //set the ID to the id of the node
          currentNode.nodeLat = Double.parseDouble(attributes.getValue(1)); //set the Lat to the Lat of the node
          currentNode.nodeLong = Double.parseDouble(attributes.getValue(2)); //set the Long to the Long of the node
          allNodes.add(currentNode);
      }       
  
      if (qName == "way") //if tag value is way
      {
          currentWay = new Way(0, null); //create a new way with 0 values
          currentWay.wayID = Integer.parseInt(attributes.getValue(0)); //set the way id to the id of the way
      //  
      }
  
      if (qName == "nd") //if tag value is nd
      {
          Node searchNode = getNodeByID(Integer.parseInt(attributes.getValue(0))); //use getNodeByID method to check if
          currentWay.containedNodes.add(searchNode);
      }
  }
  </code></pre>
  
  <p><strong>My problem:</strong></p>
  
  <p>I'm trying to create a way object that contains the ID and a list of nodes that it contains (the nd tag) the nd tags are just references to node objects that are successfully created earlier. Currently I am using 2 ArrayLists, one for the nodes and the other for ways. However the <code>getNodeByID()</code> method has to search through the list every time it hits an nd and is drastically slowing me down for larger XML files.</p>
  
  <p>I can't seem to find a way to read the nd's in with the way, and instead have to search them in a different if statement. </p>
  
  <p>Is there any way I can find a way and then in the same statement, all the nd's related to it? If so it would make creating my objects much easier as I am planning on changing those array lists to hashmaps. </p>
  
  <p>Sorry if I haven't been clear...I'm not very good at describing these problems in text.</p>