0

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.

PdM
  • 74
  • 1
  • 7
  • 2
    What is your question? The `>` does not need to be escaped since it does not signal anything special to the lexer/parser. If you really need to escape it, do it manually. – f1sh Mar 21 '19 at 12:40
  • @f1sh that's true. but is there i simple option (not rewriting marshaller/beforeMarshaller/afterMarshaller) to do this during marshalling? – PdM Mar 21 '19 at 12:45

1 Answers1

1

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:

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146