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.
我正在尝试添加XML元素,另一个XML是字符串。当我生成XML文件编码不正确并且它使我的值<> 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.
XML的结果就在这里。
<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>
2 个解决方案
#1
0
Elaborating on my comment - JAXB by default escapes any text that you set on element.
详细说明我的注释 - 默认情况下,JAXB会转义您在元素上设置的任何文本。
You can disable escaping using one of solutions described in How to prevent JAXB escaping a string
您可以使用如何防止JAXB转义字符串中描述的解决方案之一来禁用转义
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?
但是,我看到你真正需要的只是将文本放入CDATA部分。这可以使用此处描述的解决方案之一来实现:如何使用JAXB生成CDATA块?
#2
0
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.
在实现XML创建的类中,它被添加。
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.
并且完美地创建了最终的XML。
<DATOS_ESPECIFICOS><![CDATA[<nombre>pepito</nombre><apellidos>grillo</apellidos>]]></DATOS_ESPECIFICOS>
#1
0
Elaborating on my comment - JAXB by default escapes any text that you set on element.
详细说明我的注释 - 默认情况下,JAXB会转义您在元素上设置的任何文本。
You can disable escaping using one of solutions described in How to prevent JAXB escaping a string
您可以使用如何防止JAXB转义字符串中描述的解决方案之一来禁用转义
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?
但是,我看到你真正需要的只是将文本放入CDATA部分。这可以使用此处描述的解决方案之一来实现:如何使用JAXB生成CDATA块?
#2
0
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.
在实现XML创建的类中,它被添加。
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.
并且完美地创建了最终的XML。
<DATOS_ESPECIFICOS><![CDATA[<nombre>pepito</nombre><apellidos>grillo</apellidos>]]></DATOS_ESPECIFICOS>