title: Getting the information from xml from inpustream java
body: <p>I have this program that read the xml from the inputstream and this is the xml that was the result of the inputStream </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  &lt;soap:Body&gt;
      &lt;GetRouteSummaryForStopResponse xmlns="http://octranspo.com"&gt;
          &lt;GetRouteSummaryForStopResult&gt;
              &lt;StopNo xmlns="http://tempuri.org/"&gt;string&lt;/StopNo&gt;
              &lt;StopDescription 
               xmlns="http://tempuri.org/"&gt;string&lt;/StopDescription&gt;
              &lt;Error xmlns="http://tempuri.org/"&gt;string&lt;/Error&gt;
              &lt;Routes xmlns="http://tempuri.org/"&gt;
                  &lt;Route&gt;
                      &lt;RouteNo&gt;int&lt;/RouteNo&gt;
                      &lt;DirectionID&gt;int&lt;/DirectionID&gt;
                      &lt;Direction&gt;string&lt;/Direction&gt;
                      &lt;RouteHeading&gt;string&lt;/RouteHeading&gt;
                  &lt;/Route&gt;
                  &lt;Route&gt;
                      &lt;RouteNo&gt;int&lt;/RouteNo&gt;
                      &lt;DirectionID&gt;int&lt;/DirectionID&gt;
                      &lt;Direction&gt;THIS IS A STRING&lt;/Direction&gt;
                      &lt;RouteHeading&gt;string&lt;/RouteHeading&gt;
                  &lt;/Route&gt;
              &lt;/Routes&gt;
          &lt;/GetRouteSummaryForStopResult&gt;
      &lt;/GetRouteSummaryForStopResponse&gt;
  &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>I dont know where to start to get the Direction information that is in the xml.</p>
  
  <pre><code>public String HTTPrequest(String stopNo){
      String parameterURL = "appID="+this.appId+"&amp;apiKey="+this.AppKey+"&amp;routeNo=1&amp;stopNo="+stopNo+"";    
      URL url;
      HttpURLConnection connection = null;
      try {
          url = new URL(this.requestGetRouteSummaryForStop);
          connection = (HttpURLConnection) url.openConnection();
          connection.setDoOutput(true);
          connection.setDoInput(true);
          connection.setInstanceFollowRedirects(false); 
          connection.setRequestMethod("POST"); 
          connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
          connection.setRequestProperty("charset", "utf-8");
          connection.setRequestProperty("Content-Length", "" + Integer.toString(parameterURL.getBytes().length));
          connection.setUseCaches (false);
  
          //send request
           DataOutputStream wr = new DataOutputStream (
                    connection.getOutputStream ());
                    wr.writeBytes (parameterURL);
                    wr.flush ();
                    wr.close ();
  
          //Get Response  
  
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer(); 
            while((line = rd.readLine()) != null) {
              response.append(line);
              response.append('\r');
            }
            rd.close();
            System.out.print(response.toString());
        return response.toString();
      } catch (Exception e) {
          System.out.print("ERROR: ");
          e.printStackTrace();
          return null;
      }finally{
          if(connection != null){
              connection.disconnect();
          }
      }
  }
  
  
  public void parseXmlData(String response){
      InputSource xmlSource = new InputSource(new StringReader(response));
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      try {
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document document = db.parse(xmlSource);
  
          XPathFactory xPathFactory = XPathFactory.newInstance();
          XPath xPath = xPathFactory.newXPath();
  
          String routeHeading = xPath.evaluate("/GetRouteSummaryForStopResponse/GetRouteSummaryForStopResult/Routes/Route/Direction",document);
          System.out.print("RouteHeading: "+ routeHeading);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>This is the code for parsing the xml and connecting to the host</p>
  
  <p><em><strong></em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>_</strong>:EDIT:<strong><em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__<em>_</em>__</strong></p>
  
  <pre><code>    public InputStream HTTPrequest(String stopNo){
      String parameterURL = "appID="+this.appId+"&amp;apiKey="+this.AppKey+"&amp;routeNo=1&amp;stopNo="+stopNo+"";    
      URL url;
      HttpURLConnection connection = null;
      try {
          url = new URL(this.requestGetRouteSummaryForStop);
          connection = (HttpURLConnection) url.openConnection();
          connection.setDoOutput(true);
          connection.setDoInput(true);
          connection.setInstanceFollowRedirects(false); 
          connection.setRequestMethod("POST"); 
          connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
          connection.setRequestProperty("charset", "utf-8");
          connection.setRequestProperty("Content-Length", "" + Integer.toString(parameterURL.getBytes().length));
          connection.setUseCaches (false);
  
          //send request
           DataOutputStream wr = new DataOutputStream (
                    connection.getOutputStream ());
                    wr.writeBytes (parameterURL);
                    wr.flush ();
                    wr.close ();
  
          //Get Response  
  
            InputStream is = connection.getInputStream();
           return is;
      } catch (Exception e) {
          System.out.print("ERROR: ");
          e.printStackTrace();
          return null;
      }finally{
          if(connection != null){
              connection.disconnect();
          }
      }
  }
  
  
  public void parseXmlData(InputStream response){
      InputStream is = response;
  
      try {
  
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          dbf.setNamespaceAware(false);
          Document doc = dbf.newDocumentBuilder().parse(is);
  
          XPathFactory xf = XPathFactory.newInstance();
          XPath xPath = xf.newXPath();
  
          // Find direction ANY where within the document...
  
          XPathExpression xExp = xPath.compile("//DirectionID");
          NodeList nl = (NodeList) xExp.evaluate(doc, XPathConstants.NODESET);
          for (int index = 0; index &lt; nl.getLength(); index++) {
              Node node = nl.item(index);
              System.out.println(node.getTextContent());
          }
  
          // Find the direction node's any where in the document
          // where it's a child of Route, which has a child
          // node called RouteNo with the text value of 'int'...
          xExp = xPath.compile("//Route[RouteNo = 'int']/Direction");
          nl = (NodeList) xExp.evaluate(doc, XPathConstants.NODESET);
          for (int index = 0; index &lt; nl.getLength(); index++) {
              Node node = nl.item(index);
              System.out.println(node.getTextContent());
          }
  
      } catch (Exception exp) {
          exp.printStackTrace();
      }
  }
  </code></pre>
  
  <p>it give me an error of</p>
  
  <pre><code>[Fatal Error] :1:1: Premature end of file.
  org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
      at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
      at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
      at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
      at OcTranspoConnect.parseXmlData(OcTranspoConnect.java:82)
      at OcTranspoConnect.main(OcTranspoConnect.java:29)
  </code></pre>