How to rename XML attributes from Java classes using JAXB annotations?
I have this class defintion:
@XmlRootElement
public class RssRoot {
private String _version;
private String _xmlns_content;
@XmlAttribute()
public String get_version() {
return _version;
}
@XmlAttribute()
public String get_xmlns_content() {
return _xmlns_content;
}
public void set_version(String version) {
_version = version;
}
public void set_xmlns_content(String xmlnsContent) {
_xmlns_content = xmlnsCon开发者_StackOverflow中文版tent;
}
public RssRoot() {
super();
this._version = "2.0";
this._xmlns_content = "http://purl.org/rss/1.0/modules/content/";
}
}
And it generates this xml:
<rssRoot xmlnsContent="http://purl.org/rss/1.0/modules/content/" version="2.0"/>
However, I need to rename xmlnsContent
to xmlns:content
, and rssRoot
, to rss
. How can I do this?
I tried with @XmlAttribute(name = "xmlns:content")
above the getter and near to the property declaration, but no luck. The thing fails with this message:
Root Exception stack trace: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnno tationExceptions Class has two properties of the same name "_xmlns_content" this problem is related to the following location: RssRoot
What else can I do?
It may be better to use existing libraries for RSS support (such as ROME) instead of creating your own.
But if you actually want:
xmlns:content
is not an attribute, it's a namespace declaration. JAXB adds it to the resulting XML automatically when resulting XML contains elements in that namespace (namespace of elements can be specified usingnamespace
attribute in@XmlElement
,@XmlRootElement
, etc).To rename
rssRoot
torss
, write@XmlRootElement(name = "rss")
.
精彩评论