如何从SOAP响应中获取XML元素的值? (Android版) [英] How to get the value of an xml element from within a SOAP response? (Android)
问题描述
下面是SOAP响应:
<?XML版本=1.0编码=UTF-8&GT?;
< SOAP12:信封的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:身体与GT;
< SearchResponse的xmlns =...>
<&SearchResult中GT;
< XML版本=1.0独立=YES&GT?; <项目blahblahblah1< /项目>
< XML版本=1.0独立=YES&GT?; <项目blahblahblah2< /项目>
< /信息搜索结果>
< / SearchResponse>
< / SOAP12:身体与GT;
< / SOAP12:信封>
从信息搜索结果中,我想每一个项目一次完整的XML的的。我的意思是,我想要得到一个完整的项目blahblahblah /条,个别。我该怎么做呢?
下面是我已经想通了使用DOM把所有的XML从信息搜索结果中,但我如何获得项目??
的DocumentBuilder DB = factory.newDocumentBuilder();
InputSource的inStream中=新的InputSource();
inStream.setCharacterStream(新StringReader(SOA presponse));
文档的DOC = db.parse(插播广告);节点列表NL = doc.getElementsByTagName(信息搜索结果);
XML = nl.item(0).getFirstChild()getNodeValue();
一个办法是写一个通用的函数解析XML的一个已知的标记。
{
公共静态字符串parseXMLForTag(XML字符串,字符串变量){
尝试{
//创建XMLPullParserFactory&安培; XMLPullParser
XmlPullParserFactory工厂= XmlPullParserFactory.newInstance();
XmlPullParser解析器= factory.newPullParser();
parser.setInput(新StringReader(XML)); //布尔值,表明所需的标签已被发现
布尔foundTag = FALSE;
//变量来填充内容
StringBuilder的tagContents =新的StringBuilder(); //遍历文件
INT EVENTTYPE = parser.getEventType();
而(EVENTTYPE!= XmlPullParser.END_DOCUMENT){
开关(EVENTTYPE){
案例XmlPullParser.START_TAG:
如果(parser.getName()。等于(标签)){
//找到标签,开始追加到tagContents
foundTag = TRUE;
}否则如果(foundTag){
//所需标签内部新的开始标记
tagContents.append(&下;+ parser.getName()+>中);
}
打破;
案例XmlPullParser.END_TAG:
如果(parser.getName()。等于(标签)){
//为标签成品收集文本
返回tagContents.toString();
}否则如果(foundTag){
//所需标签内结束标记
tagContents.append(&下; /+ parser.getName()+>中);
}
打破;
案例XmlPullParser.TEXT:
如果(foundTag){
//所需标签内的文本
tagContents.append(parser.getText());
}
打破;
}
//获取下一个事件的类型
EVENTTYPE = parser.next();
}
返回null;
}赶上(例外五){
返回null;
}
}
}
然后,您可以使用此方法拔出搜索结果即。
字符串信息搜索结果= parseXMLForTag(回应,信息搜索结果);
和使用这个结果来分析项目
字符串项= parseXMLForTag(SearchResult中,项目);
请注意,该方法不以任何方式进行了优化,但应该为你的目的工作。
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();
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.
这篇关于如何从SOAP响应中获取XML元素的值? (Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!