When marshaling an object via JAXB using the StringBuffer attribute, this attribute becomes empty. I wrote a small program to demonstrate the problem:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class JaxbTest {
private String valueOne;
private StringBuffer valueTwo;
public static void main(String[] args) throws Exception {
JaxbTest object = new JaxbTest();
object.setValueOne("12345");
object.setValueTwo(new StringBuffer("54321"));
JAXBContext context = JAXBContext.newInstance(JaxbTest.class);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(object, System.out);
}
@XmlElement
public String getValueOne() {
return valueOne;
}
public void setValueOne(String valueOne) {
this.valueOne = valueOne;
}
@XmlElement
public StringBuffer getValueTwo() {
return valueTwo;
}
public void setValueTwo(StringBuffer valueTwo) {
this.valueTwo = valueTwo;
}
}
The output is as follows:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><jaxbTest><valueOne>12345</valueOne><valueTwo/></jaxbTest>
Does anyone know why "valueTwo" is marshaling incorrectly? BTW, I am using java 1.6.0_22.
Thanks in advance!
source
share