PdM
PdM

Reputation: 74

Java JAXB marshaller changes encoding of < but not >

having

String translationXsd = TranslationPropertyHelper.getFileLocation(PropertyKey.TRANSLATE_XSD_FILE);
  File translationXsdFile = new File(translationXsd);

  SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  Schema schema = schemaFactory.newSchema(translationXsdFile);

  JAXBContext jaxbContext = JAXBContext
      .newInstance(translationJob.getClass().getPackage().getName());
  Marshaller marshaller = jaxbContext.createMarshaller();
  OutputStream os = new FileOutputStream(pOutputFile);
  XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
  XMLStreamWriter xsw = new IndentingXMLStreamWriter(xmlof.createXMLStreamWriter(os));
  marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, translationXsdFile.getName());
  marshaller.setSchema(schema);
  marshaller.marshal(translationJob, xsw);
  xsw.close();

having a freetext, e.g. "hello i have < b > bold < / b > text inside." in node

generates

<freetextnode>hello i have &lt; b > bold &lt; / b > text inside.</freetextnode>

expectation is:

<freetextnode>hello i have &lt; b &gt; bold &lt; / b &gt; text inside.</freetextnode>

JavaEE 7.

Upvotes: 0

Views: 1812

Answers (1)

Michał Ziober
Michał Ziober

Reputation: 38720

You need to merge marshalling with com.sun.xml.internal.bind.marshaller.DumbEscapeHandler. From JavaDoc:

Escape everything above the US-ASCII code range. A fallback position. Works with any JDK, any encoding.

Simple example how to use it:

import com.sun.xml.internal.bind.marshaller.DataWriter;
import com.sun.xml.internal.bind.marshaller.DumbEscapeHandler;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import java.io.PrintWriter;

public class JaxbApp {

    public static void main(String[] args) throws Exception {
        FreeTextNode dataFile = new FreeTextNode();
        dataFile.setValue("hello i have < b > bold < / b > text inside.");
        JAXBContext jaxbContext = JAXBContext.newInstance(FreeTextNode.class);
        Marshaller marshaller = jaxbContext.createMarshaller();

        PrintWriter printWriter = new PrintWriter(System.out);
        DataWriter dataWriter = new DataWriter(printWriter, "UTF-8", DumbEscapeHandler.theInstance);
        marshaller.marshal(dataFile, dataWriter);
    }
}

@XmlRootElement(name = "freetextnode")
class FreeTextNode {

    private String value;

    @XmlValue
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

Above code prints:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<freetextnode>hello i have &lt; b &gt; bold &lt; / b &gt; text inside.</freetextnode>

See also:

Upvotes: 1

Related Questions