title: How to get a specific event/attribute content from an xml string via stAX or SAX
body: <p>I have a xml POST response which I receive as a string. I need the content of the particular "pnr" (see in xml) to pass it on to another GET request.
  I am trying sax and stAX to achieve this but failing miserably.
  I used getElementsByTagName and also getAttribute, but no go...
  Here's my code and later the xml string that I receive. </p>
  
  <p>Any kind of help will be a gift</p>
  
  <pre><code>package rest;
  import javax.xml.parsers.*;
  import org.xml.sax.InputSource;
  import org.w3c.dom.*;
  import java.io.*;
  
  
  public class ParseXMLString {
  
    public static void main(String arg[]) {
      String outputString =  RESTClient.postConfirm(); // this is the xml string response I am getting
  
      try {
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          InputSource is = new InputSource();
          is.setCharacterStream(new StringReader(outputString));
  
          Document doc = db.parse(is);
          String Parentnode = doc.getDocumentElement().getAttribute("pnr");       
  //           Element element = (Element) Parentnode.;
  //           NodeList name = element.getElementsByTagName("pnr");
  //           Element line = (Element) name.item(0);
             //String IDList = getCharacterDataFromElement(line);   
             System.out.println(Parentnode);
      }
      catch (Exception e) {
          e.printStackTrace();
      }
    }
  
    public static String getCharacterDataFromElement(Element e) {
      Node child = e.getFirstChild();
      if (child instanceof CharacterData) {
         CharacterData cd = (CharacterData) child;
         return cd.getData();
      }
      return "?";
    }
  }
  </code></pre>
  
  <p>And here is the the XML String I have received:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;Ticket&gt;&lt;bookedSeats&gt;3&lt;/bookedSeats&gt;&lt;bpAddress&gt;Anand Rao Circle&lt;/bpAddress&gt;&lt;bpLandMark&gt;ganesha temple&lt;/bpLandMark&gt;&lt;bpLocation&gt; Ghousia College&lt;/bpLocation&gt;&lt;bpPhoneNo&gt;98798679769&lt;/bpPhoneNo&gt;&lt;bpTime&gt;1200&lt;/bpTime&gt;&lt;busServiceName&gt;efdf&lt;/busServiceName&gt;&lt;busType&gt;Volvo A/C Semi Sleeper (2+2)&lt;/busType&gt;&lt;commission&gt;66.19&lt;/commission&gt;&lt;dateOfJourney&gt;2012-10-05&lt;/dateOfJourney&gt;&lt;destination&gt;Chennai&lt;/destination&gt;&lt;fare&gt;600.0&lt;/fare&gt;&lt;issueTime&gt;2012-10-04T15:46:45.073+05:30&lt;/issueTime&gt;&lt;noOfSeats&gt;1&lt;/noOfSeats&gt;&lt;passengerMobile&gt;1234567890&lt;/passengerMobile&gt;&lt;passengerName&gt;Test&lt;/passengerName&gt;&lt;pnr&gt;RATPKES44974756&lt;/pnr&gt;&lt;seatDetails&gt;&lt;seatDetail&gt;&lt;commission&gt;66.19&lt;/commission&gt;&lt;fare&gt;600.0&lt;/fare&gt;&lt;gender&gt;MALE&lt;/gender&gt;&lt;passengerAge&gt;0&lt;/passengerAge&gt;&lt;passengerMobile&gt;1234567890&lt;/passengerMobile&gt;&lt;passengerName&gt;Test&lt;/passengerName&gt;&lt;prime&gt;false&lt;/prime&gt;&lt;seatName&gt;3&lt;/seatName&gt;&lt;/seatDetail&gt;&lt;/seatDetails&gt;&lt;source&gt;Bangalore&lt;/source&gt;&lt;status&gt;BOOKED&lt;/status&gt;&lt;travelsName&gt;Rajratan Travels&lt;/travelsName&gt;&lt;/Ticket&gt;
  </code></pre>