0

Here is the SOAP response:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
    <soap12:Body>
        <SearchResponse xmlns="...">
            <SearchResult> 
                <?xml version="1.0" standalone="yes"?> <items blahblahblah1 </items> 
                <?xml version="1.0" standalone="yes"?> <items blahblahblah2 </items> 
            </SearchResult>
        </SearchResponse>
      </soap12:Body>
</soap12:Envelope>

From within "SearchResult", I want to get each of the full xmls of "items" one at a time. What I mean is, I want to get an entire "items blahblahblah /items", individually. How do I do that?

Here is what I've figured out using DOM to get all of the xml from within "SearchResult", but how do I get the items??

DocumentBuilder db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(soapResponse));
Document doc = db.parse(inStream);

NodeList nl = doc.getElementsByTagName("SearchResult");
xml = nl.item(0).getFirstChild().getNodeValue();
Kalina
  • 5,504
  • 16
  • 64
  • 101
  • 1
    Not sure what library you're using, but if it only handles soap well, then you could additionally use Jsoup to parse the inner xml. –  Sep 18 '12 at 15:01

1 Answers1

3

One approach would be to write a generic function to parse XML for a known tag. {

public static String parseXMLForTag(String xml, String tag) {
        try {
            // Create XMLPullParserFactory & XMLPullParser
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = factory.newPullParser();
            parser.setInput(new StringReader(xml));

            // boolean to indicate desired tag has been found
            boolean foundTag = false;
            // variable to fill contents
            StringBuilder tagContents = new StringBuilder();

            // loop over document
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (parser.getName().equals(tag)) {
                        // Found tag, start appending to tagContents
                        foundTag = true;
                    } else if (foundTag) {
                        // New start tag inside desired tag
                        tagContents.append("<" + parser.getName() + ">");
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if (parser.getName().equals(tag)) {
                        // Finished gathering text for tag
                        return tagContents.toString();
                    } else if (foundTag) {
                        // end tag inside desired tag
                        tagContents.append("</" + parser.getName() + ">");
                    }
                    break;
                case XmlPullParser.TEXT:
                    if (foundTag) {
                        // text inside desired tag
                        tagContents.append(parser.getText());
                    }
                    break;
                }
                // Get next event type
                eventType = parser.next();
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }
}

You could then use this method to pull out search results i.e.

String searchResult = parseXMLForTag(response, "SearchResult");

And use this result to parse items

String item = parseXMLForTag(searchResult, "item");

Note that this method is not optimized in any way and though it should work for your purpose.

swhitewvu24
  • 284
  • 1
  • 5