Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

stuck with parsing

843834Sep 9 2005 — edited Sep 9 2005
Hi everybody,

I hope you can help with what I think is for many of you a simple problem. I have been trying to understand what I am doing wrong but without success. It seems like I am following the approach that I have read on many websites... still it doesn't work. At this point 4:15 am I give up. I hope some of you can give me some tips.

The problem is with parsing an XML input stream that I get as a result from a SOAP client.

This is the format of the SOAP response
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<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/">
  <soap:Body>
    <GetQuoteResponse xmlns="http://www.webserviceX.NET/">
      <GetQuoteResult>string</GetQuoteResult>
    </GetQuoteResponse>
  </soap:Body>
</soap:Envelope>
and this is what I get when I apply the following instructions to my input stream:
int c;
while ((c = soapResponse.read()) != -1) System.out.write(c);
soapResponse.close();
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.
xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetQuoteResponse xmln
s="http://www.webserviceX.NET/"><GetQuoteResult><StockQuotes><Stock>
<Symbol>IBM</Symbol><Last>80.80</Last><Date>9/8/20
05</Date><Time>4:00pm</Time><Change>0.00</Change>&
lt;Open>N/A</Open><High>N/A</High><Low>N/A</Low>
;<Volume>0</Volume><MktCap>128.9B</MktCap><PreviousCl
ose>80.80</PreviousClose><PercentageChange>0.00%</PercentageCh
ange><AnnRange>71.85 - 99.10</AnnRange><Earns>5.119</Ear
ns><P-E>15.78</P-E><Name>INTL BUSINESS MAC</Name><
/Stock></StockQuotes></GetQuoteResult></GetQuoteResponse></soap:Body></
soap:Envelope>Error in documentParseBuilder(): Document root element is missing.

It seems like when I try to read my input stream, just to see what is inside many times I don't get the whole data structure. Am I doing some stupid mistake?

These are the methods that I use to build the XML document and to parse it. However parsing doesn't work. The list of nodes that I obtain with the instruction
NodeList nodeList = doc.getElementsByTagName("Stock");
is always of length 0, even though when it is not like that, as I can see when I print the input stream.
public static Document documentParseBuilder(InputStream soapResponse){

        Document xmlDoc = null;
        try{
            DocumentBuilderFactory parseFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder parseBuilder = parseFactory.newDocumentBuilder();

            InputSource input = new InputSource();
            input.setByteStream(soapResponse);
            input.setEncoding("UTF-8");
            xmlDoc = parseBuilder.parse(input);
        }catch(ParserConfigurationException pce){
                System.out.println("Error in  documentParseBuilder(): Parser not properly configured");
        }catch(IOException ioe){
                System.out.println("Error in documentParseBuilder(): " + ioe.getMessage());
        }catch(SAXException se){
                System.out.println("Error in documentParseBuilder(): " + se.getMessage());
        }catch(NullPointerException ne){
                System.out.println("Error in  documentParseBuilder(): Error in Response");
        }
        return xmlDoc;
    }

  private static HashMap parseResult(Document doc)
  {
      try
      {
               HashMap stockData = new HashMap(); 
               NodeList nodeList = doc.getElementsByTagName("Stock");
               String nodeName;
               String nodeValue;
               for(int i=0; i<nodeList.getLength(); i++){
	nodeName = (nodeList.item(i)).getNodeName();
          	nodeValue = (nodeList.item(i)).getNodeValue();
          	stockData.put(nodeName, nodeValue); 
               }
              return stockData;
      }
      catch(Exception e)
      {
             System.out.println(e);
      }
      return null;
  }
I have the impression that I am messing up something with the input stream.. I am sorry but it is a bit late and cannot think very clearly, plus I don't have much experience with java.

Please help ;-)

Thanks,

Pao
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 7 2005
Added on Sep 9 2005
4 comments
227 views