简体   繁体   English

Android SimpleXML库用于XML解析

[英]Android SimpleXML library for XML parsing

I am parsing one long xml using SimpleXML library. 我正在使用SimpleXML库解析一个长xml。 Here is the Link of Long XML 这是长XML的链接

http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml

Now I want guidance regarding im:image tag 现在我想要有关im:image标签的指导

I have made the following POJO class 我做了以下POJO课程

public class Image {
private int height;

public Image(@Attribute(name = "height") int height)
{
    this.height=height;
}
@Attribute(name = "height") 
public int getObjectHeight() {
    return height;
}
}

but this doesn't look me correct, because it will only deal with height...how to parse the content between these tags 但这对我来说并不正确,因为它只会处理高度...如何解析这些标签之间的内容

<im:image height="170"> </im:image>

and my second question is what should be variable name in java ... because im:image is not allowed in java. 我的第二个问题是java中的变量名应该是什么...因为在Java中不允许im:image。

Please help me ASAP. 请尽快帮我。

Thanks 谢谢

first you need to add the namespace for the element 首先,您需要为元素添加名称空间

@Namespace(reference = "http://itunes.apple.com/rss", prefix = "im")
public class Image {
   @Element(name = "image ")
   private String image_url;

   @Attribute
   private int height;
}

The next code works fine 下一个代码工作正常

@Root(name = "feed")
static class Feed {
    @Element
    Image image;

}

@Root(name = "image")
static class Image {

    @Attribute(name = "height")
    int height;

    @Text
    String content;

}

@Test
public void testPrefixedTag() throws Exception {
    String xml = 
            "<feed xmlns:im=\"http://itunes.apple.com/rss\" xmlns=\"http://www.w3.org/2005/Atom\">" +
            "<im:image height=\"170\">Content</im:image>" +
            "</feed>";
    Serializer serializer = new Persister();
    Feed feed = serializer.read(Feed.class, xml);
}

So, if you have reference to im namespace in your root tag (xml by your link has one) you can just use name of the child tags without it (as image in our case). 因此,如果您在根标记中引用了im命名空间(链接中的xml具有一个),则可以仅使用子标记的名称而不使用它(在本例中为image )。

For parsing content between tags you use @Text annotation. 要在标签之间解析内容,请使用@Text批注。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM