您正在寻找@XmlValue
注释。
我的课
package forum13626828;
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "element")
public class MyClass{
protected String elementValue;
@XmlValue
public String getElementValue() {
return elementValue;
}
public void setElementValue(String el) {
this.elementValue = el;
}
}
演示
package forum13626828;
import java.io.StringReader;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(MyClass.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StringReader xml = new StringReader("<element>data</element>");
MyClass myClass = (MyClass) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(myClass, System.out);
}
}
输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<element>data</element>