I have a service that needs to generate xml. Currently I am using jaxb and a Marshaller to create the xml using a StringWriter.
Here is the current output that I am getting.
<CompanyName>Bakery é &</CompanyName>
While this may be fine for some webservices, I need to escape special unicode characters. The service that is comsuming my xml needs to have this:
<CompanyName>Bakery é &</CompanyName>
If I use StringEscapeUtils
from commons-lang
I end up with something like the follwing. This one does not work also:
<CompanyName>Bakery &#233; &amp;</CompanyName>
Are there some settings for the Marshaller that will allow me to encode these special characters as their decimal values?
Yes, Marshaller.setProperty(jaxb.encoding,encoding) will set the encoding to use for the document. I'd guess that you want "US-ASCII".
As Ed Staub suggests, try setting the jaxb.encoding
property. The US-ASCII
encoding will cause anything above the first 128 code points to be escaped.
@XmlRootElement(name = "Company")
public class Company {
private String companyName = "Bakery \u00E9 &";
@XmlElement(name = "CompanyName")
public String getCompanyName() { return companyName; }
public void setCompanyName(String bar) { this.companyName = bar; }
public static void main(String[] args) throws Exception {
JAXBContext ctxt = JAXBContext.newInstance(Company.class);
Marshaller m = ctxt.createMarshaller();
m.setProperty("jaxb.encoding", "US-ASCII");
m.marshal(new Company(), System.out);
}
}
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