我使用JAXB marshaller将Java类转换为XML MRSS文件。看起来像这样:
public class RssBuilder {
public void saveToFile( RSS rss, String fileName ) {
JAXBContext jaxbContext = JAXBContext.newInstance( RSS.class );
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
NamespacePrefixMapper mapper = namespaceMapper;
marshaller.setProperty( "com.sun.xml.bind.namespacePrefixMapper", mapper );
marshaller.setProperty( Marshaller.JAXB_FRAGMENT, true );
fos = new FileOutputStream( fileName );
marshaller.marshal( rss, fos );
}
}
@XmlRootElement
public class RSS {
@XmlElement( name = "channel" )
public List<RssChannel> getChannels() {return channels;}
}
public class RssChannel {
@XmlElement( name = "item" )
private List<RssItem> items;
public void setItems( List<RssItem> items ) { this.items = items; }
public void addItem( RssItem item ) { this.items.add( item ); }
....
}
public class RssItem {
@XmlElement (name = "description", namespace=RSS.YAHOO_XMLNS)
private MediaTitle mediaDescription;
@XmlElement (name ="origReleaseDate", namespace=RSS.BING_XMLNS)
private String origReleaseDate;
....
}
YAHOO和BING有两种MRSS格式。 BING MRSS几乎相似,只是在YAHOO方案中添加了一些额外的元素,因此我在元素注释中使用了两个名称空间。以这种方式YAHOO MRSS文件也包含BING模式的xmnls属性的问题,尽管没有'bing'元素。我可以解决它创建单独的RSS根元素类,但它也会导致为通道和项元素创建单独的类。
有解决这个问题的简单方法吗?