, Converter Channel.
. - .. Channel Atom. , .
Channel Converter
@Root()
@Convert(Channel.ChannelConverter.class)
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;
static class ChannelConverter implements Converter<Channel>
{
@Override
public Channel read(InputNode node) throws Exception
{
Channel channel = new Channel();
InputNode child;
while( ( child = node.getNext() ) != null )
{
switch(child.getName())
{
case "title":
channel.setTitle(child.getValue());
break;
case "description":
channel.setDescription(child.getValue());
break;
case "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
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
@Root
public static class AtomLink
{
@Attribute
private String href;
@Attribute
private String rel;
@Attribute
private String type;
}
}
. - (), 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(). xmlns - XML, ; <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}}