如何在JAXB中映射只有根的XML

如何在JAXB中映射只有根的XML,jaxb,Jaxb,我想知道如何映射在JAXB中只有根元素的XML <size>4</size> 4 像这样的东西怎么样: @XmlRootElement static class Size { @XmlValue String textNode; } @Test public void jaxbRootTextNode() throws JAXBException, IOException { try (ByteArrayInputStream is =

我想知道如何映射在JAXB中只有根元素的XML

<size>4</size>
4

像这样的东西怎么样:

@XmlRootElement
static class Size {
    @XmlValue String textNode;
}


@Test
public void jaxbRootTextNode() throws JAXBException, IOException {
    try (ByteArrayInputStream is = 
            new ByteArrayInputStream("<size>4</size>"
                    .getBytes(StandardCharsets.UTF_8))) {
        JAXBContext c = JAXBContext.newInstance(Size.class);
        Unmarshaller um = c.createUnmarshaller();
        Size s = (Size) um.unmarshal(is);
        System.out.println("Text: " + s.textNode);
        System.out.println();
        s.textNode = "5";
        Marshaller m = c.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        m.marshal(s, System.out);
    }
}
@XmlRootElement
静态类大小{
@XmlValue字符串textNode;
}
@试验
public void jaxbRootTextNode()抛出jaxbeexception,IOException{
try(ByteArrayInputStream=
新ByteArrayInputStream(“4”
.getBytes(标准字符集.UTF_8))){
JAXBContext c=JAXBContext.newInstance(Size.class);
解组器um=c.createUnmarshaller();
大小s=(大小)um.unmarshal(is);
System.out.println(“Text:+s.textNode”);
System.out.println();
s、 textNode=“5”;
Marshaller m=c.createMarshaller();
m、 setProperty(Marshaller.JAXB_格式的_输出,Boolean.TRUE);
m、 setProperty(Marshaller.JAXB_编码,“UTF-8”);
m、 元帅(s,系统输出);
}
}
这张照片是:

Text: 4

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<size>5</size>
Text:4
5.