Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I wrap a section of xml in a CDATA tag using Jackson

I have the following java object

class MyXmlObject{
    @JsonProperty
    private InnerObject innerObject;
    @JsonProperty
    private String someOtherProperty;
}

When I serialize this using

public String getXmlObjectAsXML(MyXmlObject myXmlObject){
    JacksonXmlModule module = new JacksonXmlModule();

    module.setDefaultUseWrapper(false);
    XmlMapper mapper = new XmlMapper(module);

    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);

    String response = "";

    response = mapper.writeValueAsString(myXmlObject);
    return response;
}

I would like the InnerObject class to be wrapped in a CDATA tag.

What is the correct way to handle this scenario?

like image 221
AnthonyJClink Avatar asked Sep 19 '13 18:09

AnthonyJClink


People also ask

What is the correct syntax of the CDATA section in an XML document?

Syntax. CDATA End section − CDATA section ends with ]]> delimiter. CData section − Characters between these two enclosures are interpreted as characters, and not as markup. This section may contain markup characters (<, >, and &), but they are ignored by the XML processor.

What is CDATA section in XML?

A CDATA section is used to mark a section of an XML document, so that the XML parser interprets it only as character data, and not as markup. It comes handy when one XML data need to be embedded within another XML document.

How do I add CDATA to XML?

CDATA sections can appear inside element content and allow < and & character literals to appear. A CDATA section begins with the character sequence <! [CDATA[ and ends with the character sequence ]]>. Between the two character sequences, an XML processor ignores all markup characters such as <, >, and &.

Which sequence is not allowed within a CDATA section?

The only sequence which is not allowed within a CDATA section is the closing sequence of a CDATA section itself, ]]> .


1 Answers

There is @JacksonXmlCData since 2.5. https://github.com/FasterXML/jackson-dataformat-xml:

@JacksonXmlCData allows specifying that the value of a property is to be serialized within a CData tag.

like image 197
Andrey Agibalov Avatar answered Sep 18 '22 22:09

Andrey Agibalov