How should I deal with linebreaks in strings I want to marshal to XML?
I am having difficulty using Java and JAXB to handle putting strings in XML files that have linefeeds in them. The data is being pulled from a database with the actual line feed characters in them.
Foo <LF>
bar
Or an additional example:
Foo\r\n\r\nBar
Yields:
Foo

Bar
If I just marshal this data into XML, I get literal line feed characters in the output. This is apparently against XML standards where the characters should be encoded to 
. Ie in the XML file output I should see:
Foo 
bar
But if I try and do this manually, I end up with my ampersand getting encoded!
Foo &#xD;bar
This is pretty ironic because the process which is apparently supposed to encode the linebreaks in the first place and is not, is foiling my attempts to encode it manually.
Marshalling is the process of transforming Java objects into XML documents. Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API. It provides API for marshalling, unmarshalling and validating.
use <br/> ; or.
In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.
In the Java-related RFC 2713, marshalling is used when serialising objects for remote invocation. An object that is marshalled records the state of the original object and it contains the codebase (codebase here refers to a list of URLs where the object code can be loaded from, and not source code).
Below is an example of JAXB's default behaviour regarding \n
and \r
:
Java Model (Root)
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Root {
private String foo;
private String bar;
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
Demo Code
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Root root = new Root();
root.setFoo("Hello\rWorld");
root.setBar("Hello\nWorld");
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(root, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><root><bar>Hello
World</bar><foo>Hello
World</foo></root>
Below are some additional details based on some investigation that I did.
Common to All JAXB (JSR-222) Implementations
XMLStreamWriter
or XMLEventWriter
directly (via Marshaller
) or indirectly (via potentially a JAX-RS or JAX-WS provider) then the escaping will be based on the StAX implementation. Woodstox appears to escape things correctly, but the StAX implementation in the JDK I'm using did not.EclipseLink JAXB (MOXy)
\r
that I am currently in the process of fixing (see: http://bugs.eclipse.org/414608)JAXB Reference Implementation
OutputStream
, but not to a Writer
atleast in the JDK I'm using.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With