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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:59:07  来源:igfitidea点击:

How to extract XML nested tags from Soap object?

javaandroid

提问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

You can use JAXB to marshall/unmarshall the SOAP packets. I assume you have a schema for the SOAP packets that you have. Check this out - JAXB.

您可以使用 JAXB 来编组/解组 SOAP 数据包。我假设您有一个用于 SOAP 数据包的架构。看看这个 - JAXB