【问题标题】:How to get the value of an xml element from within a SOAP response? (Android)如何从 SOAP 响应中获取 xml 元素的值? (安卓)
【发布时间】:2012-09-10 20:52:53
【问题描述】:

这是 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>

从“搜索结果”中,我想一次获取“项目”的每个完整 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();

【问题讨论】:

  • 不确定你使用的是什么库,但如果它只能很好地处理soap,那么你可以另外使用Jsoup来解析内部xml。

标签: java android xml parsing soap


【解决方案1】:

一种方法是编写一个通用函数来解析已知标记的 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");

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

【讨论】:

    猜你喜欢
    • 2016-01-03
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2016-05-12
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    相关资源
    最近更新 更多