Can I force JAXB not to convert " into ", for example, when marshalling to XML?

convert java object to xml without jaxb
jaxb minimumescapehandler
jaxb set
jaxb collections
jaxb array
characterescapehandler class not found
jaxb cdata escape
jaxb custom marshaller

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 the " existed.

Even though this is normally preferred, I need my output to match a legacy system. How do I force JAXB to NOT convert the HTML entities?

--

Thank you for the replies. However, I never see the handler escape() called. Can you take a look and see what I'm doing wrong? Thanks!

package org.dc.model;

import java.io.IOException;
import java.io.Writer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.dc.generated.Shiporder;

import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;

public class PleaseWork {
    public void prettyPlease() throws JAXBException {
        Shiporder shipOrder = new Shiporder();
        shipOrder.setOrderid("Order's ID");
        shipOrder.setOrderperson("The woman said, \"How ya doin & stuff?\"");

        JAXBContext context = JAXBContext.newInstance("org.dc.generated");
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(CharacterEscapeHandler.class.getName(),
                new CharacterEscapeHandler() {
                    @Override
                    public void escape(char[] ch, int start, int length,
                            boolean isAttVal, Writer out) throws IOException {
                        out.write("Called escape for characters = " + ch.toString());
                    }
                });
        marshaller.marshal(shipOrder, System.out);
    }

    public static void main(String[] args) throws Exception {
        new PleaseWork().prettyPlease();
    }
}

--

The output is this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<shiporder orderid="Order's ID">
    <orderperson>The woman said, &quot;How ya doin &amp; stuff?&quot;</orderperson>
</shiporder>

and as you can see, the callback is never displayed. (Once I get the callback being called, I'll worry about having it actually do what I want.)

--

Solution my teammate found:

PrintWriter printWriter = new PrintWriter(new FileWriter(xmlFile));
DataWriter dataWriter = new DataWriter(printWriter, "UTF-8", DumbEscapeHandler.theInstance);
marshaller.marshal(request, dataWriter);

Instead of passing the xmlFile to marshal(), pass the DataWriter which knows both the encoding and an appropriate escape handler, if any.

Note: Since DataWriter and DumbEscapeHandler are both within the com.sun.xml.internal.bind.marshaller package, you must bootstrap javac.

Can I force JAXB not to convert " into ", for example, when , How do I force JAXB to NOT convert the HTML entities? --. Thank you for the replies. However, I never see the handler escape() called. Can you take a look and  Can I force JAXB not to convert " into &quot;, for example, when marshalling to XML? Ask Question Asked 10 years, 2 months ago. Active 1 year, 2 months ago.

I have just made my custom handler as a class like this:

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import com.sun.xml.bind.marshaller.CharacterEscapeHandler;

public class XmlCharacterHandler implements CharacterEscapeHandler {

    public void escape(char[] buf, int start, int len, boolean isAttValue,
            Writer out) throws IOException {
        StringWriter buffer = new StringWriter();

        for (int i = start; i < start + len; i++) {
            buffer.write(buf[i]);
        }

        String st = buffer.toString();

        if (!st.contains("CDATA")) {
            st = buffer.toString().replace("&", "&amp;").replace("<", "&lt;")
                .replace(">", "&gt;").replace("'", "&apos;")
                .replace("\"", "&quot;");

        }
        out.write(st);
        System.out.println(st);
    }

}

in the marshaller method simply call:

marshaller.setProperty(CharacterEscapeHandler.class.getName(),
                new XmlCharacterHandler());

it works fine.

Can I force JAXB not to convert " into ", for , Even though this is normally preferred, I need my output to match a legacy system. How do I force JAXB to NOT convert the HTML entities? --. In this article, let us see how to avoid converting to &gt ; and & to &amp ; during JAXB Marshalling operation. 1. CharacterEscapeHandler creation Create a custom Escape Handler by implementing the CharacterEscapeHandler interface as given below.

I've been playing with your example a bit and debugging the JAXB code. And it seems it's something specific about UTF-8 encoding used. The escapeHandler property of MarshallerImpl seems to be set properly. However it's being used not in every context. If I searched for calls of MarshallerImpl.createEscapeHandler() I found:

public XmlOutput createWriter( OutputStream os, String encoding ) throws JAXBException {
    // UTF8XmlOutput does buffering on its own, and
    // otherwise createWriter(Writer) inserts a buffering,
    // so no point in doing a buffering here.

    if(encoding.equals("UTF-8")) {
        Encoded[] table = context.getUTF8NameTable();
        final UTF8XmlOutput out;
        if(isFormattedOutput())
            out = new IndentingUTF8XmlOutput(os,indent,table);
        else {
            if(c14nSupport)
                out = new C14nXmlOutput(os,table,context.c14nSupport);
            else
                out = new UTF8XmlOutput(os,table);
        }
        if(header!=null)
            out.setHeader(header);
        return out;
    }

    try {
        return createWriter(
            new OutputStreamWriter(os,getJavaEncoding(encoding)),
            encoding );
    } catch( UnsupportedEncodingException e ) {
        throw new MarshalException(
            Messages.UNSUPPORTED_ENCODING.format(encoding),
            e );
    }
}

Note that in your setup the top section (...equals("UTF-8")...) is taken into consideration. However this one doesn't take the escapeHandler. However if you set the encoding to any other, the bottom part of this method is called (createWriter(OutputStream, String)) and this one uses escapeHandler, so EH plays its role. So, adding...

    marshaller.setProperty(Marshaller.JAXB_ENCODING, "ASCII");

makes your custom CharacterEscapeHandler be called. Not really sure, but I would guess this is kind of bug in JAXB.

java Can I force JAXB not to convert, java Can I force JAXB not to convert " into ", for example, when marshalling to XML? Question. All Note Code Video Articles. It is required by JAX-B, without this, the object cannot be converted into xml. @XmlAccessorType: This tells JAX-B witch access level is used. NONE: Indicates that JAX-B will not convert any properties. FIELD: Indicates that JAX-B will search for annotations on field level.

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);
    }
});

Can I force JAXB not to convert " into ", for example , How do I force JAXB to NOT convert the HTML entities? --. Thank you for the replies. However, I never see the handler escape() called. Can you take a look and  A new Java API called Java Architecture for XML Binding (JAXB) can make it easier to access XML documents from applications written in the Java programming language. Java developers can invoke a SAX or DOM parser in an application through the JAXP API to parse an XML document -- that is, scan the document and logically break it up into discrete

@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"));

Marshaller (Java Platform SE 7 ), Writer. Use the setProperty API to change the output encoding used during these marshal The Marshaller will use "UTF-8" by default if this property is not specified. Node) to force a deep copy of the content tree to a DOM representation. In this article I'll show you how to make use of the Java Architecture for XML Binding - JAXB without using the XML Schema Definition file. JAXB is used for marshalling and unmarshalling Plain Old Java Objects to & from XML files. Although a Binding compiler can be used to generate JAXB compliant classes, with…

JAXBContext (Java Platform SE 7 ), It does not apply to JAXB annotated classes. This phase of the look up allows some packages to force the use of a certain JAXB implementation. Create an Unmarshaller object that can be used to convert XML data into a java content tree​. When handling complex data types that may not be directly available in JAXB, we may write an adapter to indicate JAXB how to manage a specific type. Using JAXB's XmlAdapter , we may define a custom code to convert an unmappable class into something that JAXB can handle.

Can I force JAXB not to convert \" into \", for example, when , Even though this is normally preferred, I need my output to match a legacy system. How do I force JAXB to NOT convert the HTML entities? --. I read your blog, nice and concise. If you're not against some command line magic here's an openssl command you can run to convert your private key & public key into a PFX file without using an online service. openssl pkcs12 -export -inkey private.key -in certificate.crt -name friendlyname -out cert.pfx -passout pass:passwordgoeshere

Controlling character escaping with jaxb – CoderLeaf, How do I force JAXB to NOT convert the HTML entities? --. Thank you for the replies. However, I never see the handler escape() called. Can you take a look and  Can I force JAXB not to convert " into ", for example, when marshalling to XML? stackoverflow.com. I have an Object that is being marshalled to XML using JAXB. One element contains a String that includes quotes ("). The resulting XML has &quot; where the "

Comments
  • Deleted my prior answer, since it was utterly wrong... however, it's still worth pointing out that &quot; is not an HTML entity, it's an XML escape.
  • It's actually both an XML and HTML entity. en.wikipedia.org/wiki/…
  • @Elliot: but in this context it is an XML escape. This is not HTML.
  • A teammate of mine figured this out without requiring a Vendor Specific implementation. Shown above.
  • I've run into the same problem. Which version of JAXB are you using? Currently I use JAXB 2.2.4 and in that release quotes are escaped only in attributes. However I have some XML files, produced by v2.1.13, which have the same "problem". However I have checked the source of MinimumEscapeHandler from 2.1.13, and it seems to be OK (I even checked 2.0.1). Perhaps, this escape handler was not activated (thanks to @GrzegorzOledzki for bug report).
  • Did you try @laz's answer? That looks like the way to do it "properly".
  • The above method works - instead of using properties to set the escape handler in jdk 1.6.0.22
  • This one works for Java 7 instead of setting handler property to marshaller, the problem is how to format the output of the datawriter now
  • This does not work in Java 1.7, it is an exploit of internal structures.
  • Super like Laura :)
  • import com.sun.xml.bind.marshaller.CharacterEscapeHandler; xm.bind not found
  • Thanks for your response, Grzegorz. I agree with you, it appears to be a JAXB bug. And if there is a legitimate reason for it, it'd be nice to have it in the documentation. Thanks!