I have an Object that is being marshalled to XML using JAXB. One element contains a String that includes quotes (\"). The resulting XML has "
where t
I checked the XML specification. http://www.w3.org/TR/REC-xml/#sec-references says "well-formed documents need not declare any of the following entities: amp, lt, gt, apos, quot. " so it appears that the XML parser used by the legacy system is not conformant.
(I know that it does not solve your problem, but it is at least nice to be able to say which component is broken).
i found same issue i fixed this using xmlWriter in xmlWriter file there is one method isEscapeText() and setEscapeTest that is by default true if you dont want transformation between < to < that time you need to setEscapeTest(false); during marshalling
JAXBContext jaxbContext = JAXBContext.newInstance(your class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Create a filter that will remove the xmlns attribute
NamespaceFilter outFilter = new NamespaceFilter(null, false);
// Do some formatting, this is obviously optional and may effect
// performance
OutputFormat format = new OutputFormat();
format.setIndent(true);
format.setNewlines(true);
// Create a new org.dom4j.io.XMLWriter that will serve as the
// ContentHandler for our filter.
XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
writer.setEscapeText(false); // <----------------- this line
// Attach the writer to the filter
outFilter.setContentHandler(writer);
// marshalling
marshaller.marshal(piaDto, outFilter);
marshaller.marshal(piaDto, System.out);
this change writer.setEscapeText(false); fixed my issue hope this changes helpful to you
I would say that easiest way to do is by overriding CharacterEscapeHandler
:
marshaller.setProperty("com.sun.xml.bind.characterEscapeHandler", new CharacterEscapeHandler() {
@Override
public void escape(char[] ch, int start, int length, boolean isAttVal,
Writer out) throws IOException {
out.write(ch, start, length);
}
});
@Elliot you can use this in order to enable marshaller to enter characterEscape function. It is wierd but it works if you set "Unicode" instead of "UTF-8". Add this just before or after you set CharacterEscapeHandler property.
marshaller.setProperty(Marshaller.JAXB_ENCODING, "Unicode");
However don't be sure just only by checking your console within your IDE, because it should be shown depend on the workspace encoding. It is better to check it also from a file like that:
marshaller.marshal(shipOrder, new File("C:\\shipOrder.txt"));
I would advise against using CharacterEscapeHandler
for the reasons mentioned above (it's an internal class). Instead you can use Woodstox and supply your own EscapingWriterFactory
to a XMLStreamWriter
. Something like:
XMLOutputFactory2 xmlOutputFactory = (XMLOutputFactory2)XMLOutputFactory.newFactory();
xmlOutputFactory.setProperty(XMLOutputFactory2.P_TEXT_ESCAPER, new EscapingWriterFactory() {
@Override
public Writer createEscapingWriterFor(Writer w, String enc) {
return new EscapingWriter(w);
}
@Override
public Writer createEscapingWriterFor(OutputStream out, String enc) throws UnsupportedEncodingException {
return new EscapingWriter(new OutputStreamWriter(out, enc));
}
});
marshaller.marshal(model, xmlOutputFactory.createXMLStreamWriter(out);
An example of how to write an EscapingWriter
can be seen in CharacterEscapingTest.
Seems like it is possible with Sun's JAXB implementation, although I've not done it myself.