0

这是 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”中,我想一次获取“项目”的每个完整 xml。我的意思是,我想单独获得一个完整的“items 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();
4

1 回答 1

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

请注意,此方法未以任何方式进行优化,尽管它应该适合您的目的。

于 2012-09-18T16:03:39.223 回答