1

我正在使用 SimpleXML 库解析一个长 xml。这是Long XML的链接

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

现在我想要关于 im:image 标签的指导

我做了以下 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;
}
}

但这看起来不正确,因为它只会处理高度......如何解析这些标签之间的内容

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

我的第二个问题是java中的变量名应该是什么......因为java中不允许使用im:image。

请尽快帮助我。

谢谢

4

2 回答 2

0

首先你需要为元素添加命名空间

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

   @Attribute
   private int height;
}
于 2013-10-02T19:04:18.637 回答
0

下一个代码工作正常

@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);
}

因此,如果您im在根标签中引用了命名空间(链接中的 xml 有一个),您可以只使用没有它的子标签的名称(如image我们的例子)。

为了解析标签之间的内容,您使用@Text注释。

于 2016-03-04T11:52:34.427 回答