Java 如何使用Jackson解析xmlElements?
如何使用Jackson解析xmlElements? 例如,我的xml是:Java 如何使用Jackson解析xmlElements?,java,android,xml,parsing,jackson,Java,Android,Xml,Parsing,Jackson,如何使用Jackson解析xmlElements? 例如,我的xml是: <channel> <title>Title</title> <link>http://example.com</link> <item> <category>Categ</category> <guid>http://1294796429.html</guid>
<channel>
<title>Title</title>
<link>http://example.com</link>
<item>
<category>Categ</category>
<guid>http://1294796429.html</guid>
<rian:priority xmlns:rian="http://example.com">3</rian:priority>
</item>
...
<item>... </item>
...
</channel>
但它不起作用。错误是
无法将字符串保存到项只需创建类
@JacksonXmlRootElement(localName = "channel")
public class Channel
{
private String title;
private String link;
public Channel(){
}
@JacksonXmlProperty(localName = "item")
@JacksonXmlElementWrapper(useWrapping = false)
public List<Item> items;
//----getters...settters
}
}
跑
只需要创建一个类
@JacksonXmlRootElement(localName = "channel")
public class Channel
{
private String title;
private String link;
public Channel(){
}
@JacksonXmlProperty(localName = "item")
@JacksonXmlElementWrapper(useWrapping = false)
public List<Item> items;
//----getters...settters
}
}
跑
@JacksonXmlRootElement(localName = "channel")
public class Channel
{
private String title;
private String link;
public Channel(){
}
@JacksonXmlProperty(localName = "item")
@JacksonXmlElementWrapper(useWrapping = false)
public List<Item> items;
//----getters...settters
}
public class Item {
private String guid;
private String category;
public Item(){
}
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
XmlMapper xmlMapper = new XmlMapper(module);
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Channel mChannel = xmlMapper.readValue(in, Channel.class);
Log.e(LOG_TAG, "getItemsSize: " + mChannel.getItems().size());