@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>