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><![CDATA[<nombre>pepito</nombre><apellidos>grillo</apellidos>]]>;</DATOS_ESPECIFICOS>
And the result i will get is .....
<DATOS_ESPECIFICOS><![CDATA[<nombre>pepito</nombre><apellidos>grillo</apellidos>]]></DATOS_ESPECIFICOS>
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?