简体   繁体   English

JAXB-具有xml值的封送字符串,但不转义该值

[英]JAXB - marshal string with xml value without the value getting escaped

I'm trying to marshal a String that contains xml. 我正在尝试封送包含xml的字符串。 For example: 例如:

<articles>
 <article>
  <name>foo</name>
  <price>5</price>
 </article>
</articles

The result I get is always an escaped String: 我得到的结果总是一个转义的字符串:

&lt;articles&gt;&lt;article&gt;&lt;name&gt;foo&lt;/name&gt;&lt;price&gt;5&lt;/price&gt;&lt;/article&gt;&lt;/articles&gt;

I created a test which looks like this: 我创建了一个看起来像这样的测试:

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;
    }
}

I've tried a lot of suggestions from stackoverflow, but none of them prevent the escaping. 我已经尝试了很多来自stackoverflow的建议,但是没有一个可以阻止转义。 Trying a handler (for @XmlAnyElement) ends with "unable to marshal type 'java.lang.String' as an element because it is missing an @XmlRootElement". 尝试处理程序(对于@XmlAnyElement)以“无法将类型'java.lang.String'作为元素封送,因为它缺少@XmlRootElement”而结束。

I'm new to JAXB, so if anyone could help me get through this, you would make my day. 我是JAXB的新手,因此,如果有人可以帮助我度过这一难关,那么您会过得开心。

If you want to use XML in XML you should use CDATA, see CDATA Sections . 如果要在XML中使用XML,则应使用CDATA, 请参阅CDATA部分 Anything else won't work when marshalling with JAXB. 与JAXB一起编组时,其他任何操作均无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM