java 如何从 Soap 对象中提取 XML 嵌套标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15826926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract XML nested tags from Soap object?
提问by yakusha
Here is my requirement,
这是我的要求,
I am getting response from the server as a soap object, i converted it to string format, now i want to extract the xml data,
我从服务器得到响应作为一个肥皂对象,我将它转换为字符串格式,现在我想提取 xml 数据,
How can i extract the nested tags?
如何提取嵌套标签?
Can I extract all the data or do i need to parse that XML response? Is there any way to extract the XML nested tags and parse it to ArrayList<> Where post is a class?
我可以提取所有数据还是需要解析该 XML 响应?有没有办法提取XML嵌套标签并将其解析为 ArrayList<> where post is a class?
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<maintag>
<item>
<name>AndroidPeople</name>
<website category="android">www.androidpeople.com</website>
</item>
<item>
<name>iPhoneAppDeveloper</name>
<website category="iPhone">www.iphone-app-developer.com</website>
</item>
</maintag>
..
If possible how?
如果可能怎么办?
Thanks in Advance.
提前致谢。
i saw this is it possible to use it and how
我看到这是否可以使用它以及如何使用
private static Show getShowFromRSS(Context context, Document feed,
String feedUrl) {
try {
// There should be one channel in the feed, get it.
// Also, the cast should be okay if the XML is formatted correctly
NodeList item = feed.getElementsByTagName("channel");
Element el = (Element)item.item(0);
String title;
NodeList titleNode = el.getElementsByTagName("title");
if (titleNode == null || titleNode.getLength() < 1)
title = context.getString(R.string.default_title);
else
title = titleNode.item(0).getFirstChild().getNodeValue();
String author;
NodeList authorNode = el.getElementsByTagName("author");
if (authorNode == null || authorNode.getLength() < 1)
author = context.getString(R.string.default_author);
else
author = authorNode.item(0).getFirstChild().getNodeValue();
String desc;
NodeList descNode = el.getElementsByTagName("comments");
if (descNode == null || descNode.getLength() < 1)
desc = context.getString(R.string.default_comments);
else
desc = descNode.item(0).getFirstChild().getNodeValue();
String imageUrl;
NodeList imagNode = el.getElementsByTagName("image");
if(imagNode != null) {
Element ima = (Element)imagNode.item(0);
if (ima != null) {
NodeList urlNode = ima.getElementsByTagName("url");
if(urlNode == null || urlNode.getLength() < 1)
imageUrl = null;
else
imageUrl =
urlNode.item(0).getFirstChild().getNodeValue();
} else
imageUrl = null;
} else
imageUrl = null;
return new Show(title, author, feedUrl, desc, imageUrl, -1, -1);
} catch (Exception e) {
// Any parse errors and we'll log and fail
Log.e("NCRSS", "Error parsing RSS", e);
return null;
}
}
回答by Kishore
回答by Kishore
You can parse your SOAP Object like normal XML, (SAX, etc.) Check out this tutorial: http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
您可以像普通 XML(SAX 等)一样解析 SOAP 对象,请查看本教程:http: //www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser /
With XPath
:
Soap response message parsing using XPath
使用XPath
:
使用 XPath 解析 Soap 响应消息
and this: Java library to form and parse soap messages
and this: http://www.coderanch.com/t/520979/XML/parse-SOAP-Xml-java-code
和这个:http: //www.coderanch.com/t/520979/XML/parse-SOAP-Xml-java-code