我需要将XML字符串解析为对象。我会使用SimpleXML,但出现错误Duplicate annotation of name 'link' on field 'url' private java.lang.String com.example.rogedormans.xmlreader.XML.Alert.Channel.url
。
具有相同问题的示例XML:
<rss........>
<channel>
<title>The Title</title>
<link>http://www.someurl.com</link>
<description>Some description</description>
<atom:link href="http://dunno.com/rss.xml" rel="self" type="application/rss+xml"/>
....
....
</channel>
</rss>
我在Google上搜索了很多,发现This,this和this stackoverflow文章,但没有一个对我有用...我也读过Simple XML documentation,但我无法使它工作。
如何将两个“链接”项都放入我的对象中? (我认为这与 namespace 有关,但是呢?)
一个代码示例会很好!!!
最佳答案
您可以通过为Channel类实现 Converter
来解决此问题。
这是给你的例子。它没有任何类型的错误检查等,并且仅使用单个Channel
简化为Atom
类。但是您会看到它是如何工作的。
类Channel
与Converter
@Root()
@Convert(Channel.ChannelConverter.class) // Specify the Converter
public class Channel
{
@Element
private String title;
@Element
private String link;
@Element
private String description;
@Namespace(reference = "http://www.w3.org/2005/Atom", prefix = "atom")
@Element()
private AtomLink atomLink;
// Ctor's / getter / setter ...
static class ChannelConverter implements Converter<Channel>
{
@Override
public Channel read(InputNode node) throws Exception
{
Channel channel = new Channel();
InputNode child;
// Iterate over all childs an get their values
while( ( child = node.getNext() ) != null )
{
switch(child.getName())
{
case "title":
channel.setTitle(child.getValue());
break;
case "description":
channel.setDescription(child.getValue());
break;
case "link":
/*
* "link" can be either a <link>...</link> or
* a <atom:link>...</atom:link>
*/
if( child.getPrefix().equals("atom") == true )
{
AtomLink atom = new AtomLink();
atom.setHref(child.getAttribute("href").getValue());
atom.setRel(child.getAttribute("rel").getValue());
atom.setType(child.getAttribute("type").getValue());
channel.setAtomLink(atom);
}
else
{
channel.setLink(child.getValue());
}
break;
default:
throw new RuntimeException("Unknown Element found: " + child);
}
}
return channel;
}
@Override
public void write(OutputNode node, Channel value) throws Exception
{
/*
* TODO: Implement if necessary
*/
throw new UnsupportedOperationException("Not supported yet.");
}
}
@Root
public static class AtomLink
{
@Attribute
private String href;
@Attribute
private String rel;
@Attribute
private String type;
// Ctor's / getter / setter ...
}
}
所有内部类也可以像平常一样实现。如果事情很复杂(反序列化),您也可以在
Serializer
中使用Converter
。用法
最后是一个演示:
final String xml = " <channel>\n"
+ " <title>The Title</title>\n"
+ " <link>http://www.someurl.com</link>\n"
+ " <description>Some description</description>\n"
+ " <atom:link href=\"http://dunno.com/rss.xml\" rel=\"self\" type=\"application/rss+xml\" xmlns:atom=\"http://www.w3.org/2005/Atom\" />\n"
+ " ....\n"
+ " ....\n"
+ " </channel>\n";
Serializer ser = new Persister(new AnnotationStrategy());
// ^----- Important! -----^
Channel c = ser.read(Channel.class, xml);
System.out.println(c);
请注意,
Converter
需要一个Strategy
(有关更多详细信息,请参见上面的链接);我使用了AnnotationStrategy
,因为那时您可以简单地使用@Convert()
。必须在XML中的某个位置定义xmlns
,否则您将捕获到Exception;我把它放到这里的<atom:link … />
中。输出
Channel{title=The Title, link=http://www.someurl.com, description=Some description, atomLink=AtomLink{href=http://dunno.com/rss.xml, rel=self, type=application/rss+xml}}