Hello,
I want to ask you a question. I have a class:
@XmlRootElement(name="test")
public class Test {
private String val;
public String getValue() {
return val;
}
public void setValue(String value) {
this.val = value;
}
}
I create a marshaller with the code bellow:
JAXBContext jc = JAXBContext.newInstance(Test.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
marshaller.setProperty( "com.sun.xml.bind.characterEscapeHandler", new
CharacterEscapeHandler() {
public void escape(char[] ch, int start, int length, boolean isAttVal,
Writer out) throws IOException {
String s = new String(ch, start, length);
System.out.println("Inside CharacterEscapeHandler...");
out.write(StringEscapeUtils.escapeXml(StringEscapeUtils.unescapeXml(s)));
}
});
Test test = new Test();
test.setValue("абв");
StringWriter writer = new StringWriter();
marshaller.marshal(test, writer);
System.out.println(writer);
The output is:
Inside CharacterEscapeHandler...
Inside CharacterEscapeHandler...
Inside CharacterEscapeHandler...
Inside CharacterEscapeHandler...
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test>
<value>абв</value>
</test>
Now everithing is fine. The code above invokes CharacterEscapeHandler.escape()
method, and the characters are escaped....
but the problem is with the following code:
SOAPMessage message = MessageFactory.newInstance().createMessage();
marshaller.marshal(test, message.getSOAPBody());
message.writeTo(System.out);
The output is:
абв
Now the text between <test></test> tag is not escaped, and the
CharacterEscapeHandler.escape() is not Invoked!!!!
Is this normal behaviour of JAXB's marshaller, and if not - how can I escape xml
inside SOAP's body?
Best regards,
Georgi Georgiev