Java JAXB-使用xml值封送字符串,而不转义该值

Java JAXB-使用xml值封送字符串,而不转义该值,java,jaxb,Java,Jaxb,我正在尝试封送包含xml的字符串。 例如: <articles> <article> <name>foo</name> <price>5</price> </article> </articles 我创建了一个如下所示的测试: public class XmlTest { public static void main(String[] args) { Flex fl

我正在尝试封送包含xml的字符串。 例如:

<articles>
 <article>
  <name>foo</name>
  <price>5</price>
 </article>
</articles
我创建了一个如下所示的测试:

public class XmlTest {
    public static void main(String[] args) {

        Flex flex = new Flex();
        flex.setValueA("lalala");
        flex.setValueB("<articles><article><name>foo</name><price>5</price></article></articles>");

        JAXBElement<Flex> el = new JAXBElement(new QName("Blub"), flex.getClass(), flex);

        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Flex.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(flex, System.out);

            JAXBContext jaxbContext2 = JAXBContext.newInstance(Flex.class);
            Marshaller marshaller2 = jaxbContext2.createMarshaller();
            marshaller2.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller2.marshal(el, System.out);
        } catch (JAXBException e) {
            System.out.println(e);
        }
    }

    @XmlRootElement(name = "foo")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Flex extends AuditableVo {

        @XmlElement(name = "valueA")
        @Getter
        @Setter
        private String valueA;

        @XmlElement(name = "valueB")
        @Getter
        @Setter
        private String valueB;
    }
}
公共类XmlTest{
公共静态void main(字符串[]args){
Flex=新Flex();
flex.setValueA(“lalala”);
flex.setValueB(“foo5”);
JAXBElement el=newjaxbelement(newqname(“Blub”),flex.getClass(),flex);
试一试{
JAXBContext JAXBContext=JAXBContext.newInstance(Flex.class);
Marshaller=jaxbContext.createMarshaller();
setProperty(marshaller.JAXB_格式化的_输出,true);
marshaller.marshall(flex,System.out);
JAXBContext jaxbContext2=JAXBContext.newInstance(Flex.class);
Marshaller marshaller2=jaxbContext2.createMarshaller();
setProperty(Marshaller.JAXB_格式化的_输出,true);
元帅2.元帅(el,系统输出);
}捕获(JAXBEException e){
系统输出打印ln(e);
}
}
@XmlRootElement(name=“foo”)
@XmlAccessorType(XmlAccessType.FIELD)
公共静态类Flex扩展了AuditableVo{
@xmlement(name=“valueA”)
@吸气剂
@塞特
私有字符串值a;
@xmlement(name=“valueB”)
@吸气剂
@塞特
私有字符串值b;
}
}
我试过stackoverflow的很多建议,但没有一个能阻止逃逸。尝试处理程序(针对@XmlAnyElement)以“无法将类型'java.lang.String'封送为元素,因为它缺少@XmlRootElement”结尾


我是JAXB新手,所以如果有人能帮我度过难关,你会让我开心的。

如果你想在XML中使用XML,你应该使用CDATA。使用JAXB进行编组时,其他任何操作都不起作用。

谢谢,我将对此进行研究。此时,我刚刚将标记复制到字符串->flex.setValueB(“Hello,world!]]>”;而且它仍然逃避价值。
public class XmlTest {
    public static void main(String[] args) {

        Flex flex = new Flex();
        flex.setValueA("lalala");
        flex.setValueB("<articles><article><name>foo</name><price>5</price></article></articles>");

        JAXBElement<Flex> el = new JAXBElement(new QName("Blub"), flex.getClass(), flex);

        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Flex.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(flex, System.out);

            JAXBContext jaxbContext2 = JAXBContext.newInstance(Flex.class);
            Marshaller marshaller2 = jaxbContext2.createMarshaller();
            marshaller2.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller2.marshal(el, System.out);
        } catch (JAXBException e) {
            System.out.println(e);
        }
    }

    @XmlRootElement(name = "foo")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Flex extends AuditableVo {

        @XmlElement(name = "valueA")
        @Getter
        @Setter
        private String valueA;

        @XmlElement(name = "valueB")
        @Getter
        @Setter
        private String valueB;
    }
}