JAXB How to append and XML String into a XML element?

2.3k views Asked by At

I'm trying to add an element of XML , another XML that is a string . The problem comes when I generate the XML file coding is not correct and it makes me values ​​< > HTML.

JAXBContext jaxbContext = JAXBContext.newInstance(ExpedienteType.class);

String XMLDatosEspecificos = "<![CDATA[" + XMLDatosEspecificos + "]]>";

expedienteType.setDATOSESPECIFICOS(XMLDatosEspecificos);

Marshaller marshaller = jaxbContext.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);

marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");

JAXBElement<ExpedienteType> jaxbElement = new JAXBElement<ExpedienteType>(
new QName("", "Expediente"), ExpedienteType.class, expedienteType);

ByteArrayOutputStream os = new ByteArrayOutputStream();

marshaller.marshal(jaxbElement, os);

File f = new File("file.xml");
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
fos.write(os.toByteArray());
os.close();
fos.close();

The result of XML is here.

<DATOS_ESPECIFICOS>&lt;![CDATA[&lt;nombre&gt;pepito&lt;/nombre&gt;&lt;apellidos&gt;grillo&lt;/apellidos&gt;]]>;</DATOS_ESPECIFICOS>

And the result i will get is .....

<DATOS_ESPECIFICOS><![CDATA[<nombre>pepito</nombre><apellidos>grillo</apellidos>]]></DATOS_ESPECIFICOS>
2

There are 2 answers

0
Dawid Pytel On BEST ANSWER

Elaborating on my comment - JAXB by default escapes any text that you set on element.

You can disable escaping using one of solutions described in How to prevent JAXB escaping a string

However, I see that what you really need is just putting text in CDATA section. This can be achieved using one of solutions described here: How to generate CDATA block using JAXB?

0
Ricardo López García On

the best solution is the one I implemented . There is more to create a handler who writes everything that happens without making any changes so that text is written without changing any character.

import java.io.IOException;
import java.io.Writer;

import com.sun.xml.bind.marshaller.CharacterEscapeHandler;

public class MyEscapeHandler implements CharacterEscapeHandler {

    @Override
    public void escape(char[] ch, int start, int length, boolean isAttVal,
            Writer out) throws IOException {
        out.write(ch, start, length);
    }

}

in the class which is implementing the creation of XML it is added.

JAXBContext jaxbContext = JAXBContext
                        .newInstance(ExpedienteType.class);

                Marshaller marshaller = jaxbContext.createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                        true);
                marshaller.setProperty(Marshaller.JAXB_ENCODING,
                        "ISO-8859-1");
                StringBuffer strBuff = new StringBuffer("");
                strBuff.append("PLAT2_");
                strBuff.append(expedienteType.getNEXPEDIENTE().replace("/", "_"));


                CharacterEscapeHandler escapeHandler = new MyEscapeHandler();
                marshaller.setProperty("com.sun.xml.bind.characterEscapeHandler", escapeHandler); 

                JAXBElement<ExpedienteType> jaxbElement = new JAXBElement<ExpedienteType>(
                        new QName("", "Expediente"), ExpedienteType.class,
                        expedienteType);
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                marshaller.marshal(jaxbElement, os);

and the final XML is perfectly created.

 <DATOS_ESPECIFICOS><![CDATA[<nombre>pepito</nombre><apellidos>grillo</apellidos>]]></DATOS_ESPECIFICOS>