Please see full example on http://technicalminds.in/core-java/jaxb/jaxb-hello-world-example/
JAXB provides default escaping while doing the marshalling process.
But there are situations when we don't want to escape the special characters.
To explain more about this, i will reuse the same example explained in the above post. For example you want to show the name as "Sivaranjani <Java Developer>"
Normal Approach and Output
Customer.Java
package jaxb;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
public class Marshalling {
public static void main(String args[])throws JAXBException{
/**
* Create Customer Java Object
*/
Customer customer = new Customer();
customer.setFisrtName("Sivaranjani <Java Developer>");
customer.setLastName("Rajadurai");
customer.setEmailAddress("siva@gmail.com");
customer.setDob(new Date());/**
* Creating Jaxb Context and Marshaller
*/
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);/**
* Creates the xml output in the console
*/
marshaller.marshal(customer, System.out);/**
* Create xml output in a file
*/
File file = new File("JaxbXml.xml");
marshaller.marshal(customer, file);
}
}
class JaxbCharacterEscapeHandler implements CharacterEscapeHandler {
public void escape(char[] buf, int start, int len, boolean isAttValue,
Writer out) throws IOException {
for (int i = start; i < start + len; i++) {
char ch = buf[i];
out.write(ch);
}
}
}
and the output file will be,
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer>
<dob>2014-01-29T18:01:33.047+05:30</dob>
<emailAddress>siva@gmail.com</emailAddress>
<fisrtName>Sivaranjani <Java Developer></fisrtName>
<lastName>Rajadurai</lastName>
</customer>
So in the above output, the angular brackets in first name are replaced with special characters. So to avoid that, we have to use the CharacterEscapeHandler provided by JAXB.
Steps:
- Create a class that implements the
com.sun.xml.bind.marshaller.CharacterEscapeHandler
interface. - Create an instance of that.
- Set that instance in the marshaller's Marshaller.JAXB_ENCODING property.
CharacterEscapeHandler Approach:
package jaxb;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
public class Marshalling {
public static void main(String args[])throws JAXBException{
/**
* Create Customer Java Object
*/
Customer customer = new Customer();
customer.setFisrtName("Sivaranjani <Java Developer>");
customer.setLastName("Rajadurai");
customer.setEmailAddress("siva@gmail.com");
customer.setDob(new Date());/**
* Creating Jaxb Context and Marshaller
*/
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);/**
* Creates the xml output in the console
*/
marshaller.marshal(customer, System.out);
/**
* Code to unescape special character while marshalling
*/
marshaller.setProperty(Marshaller.JAXB_ENCODING, "US-ASCII");
CharacterEscapeHandler escapeHandler = new JaxbCharacterEscapeHandler();
marshaller.setProperty("com.sun.xml.bind.characterEscapeHandler", escapeHandler);
/**
* Create xml output in a file
*/
File file = new File("JaxbXml.xml");
marshaller.marshal(customer, file);
}
}
class JaxbCharacterEscapeHandler implements CharacterEscapeHandler {
public void escape(char[] buf, int start, int len, boolean isAttValue,
Writer out) throws IOException {
for (int i = start; i < start + len; i++) {
char ch = buf[i];
out.write(ch);
}
}
}
Now see the output,
<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<customer>
<dob>2014-01-29T18:10:13.916+05:30</dob>
<emailAddress>siva@gmail.com</emailAddress>
<fisrtName>Sivaranjani <Java Developer></fisrtName>
<lastName>Rajadurai</lastName>
</customer>
Limitations:
- It won't work when you set the Marshaller.JAXB_ENCODING as "UTF-8". It is bug reported on JIRA
- unmarshalling wont work with the XML file generated.