如何从SOAP响应中获取xml元素的值? (机器人)

时间:2012-09-18 14:57:44

标签: java android xml parsing soap

以下是SOAP响应:

<?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>

在“SearchResult”中,我希望一次获得一个完整xmls的“项目”。我的意思是,我希望单独获得整个“物品blahblahblah / items”。我该怎么做?

以下是我使用DOM从“SearchResult”中获取所有xml的结果,但是如何获取这些项目?

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();

1 个答案:

答案 0 :(得分:3)

一种方法是编写通用函数来解析已知标记的XML。 {

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;
        }
    }
}

然后您可以使用此方法提取搜索结果,即

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

并使用此结果来解析项目

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

请注意,此方法未以任何方式进行优化,尽管它可以用于您的目的。