Вы можете использовать apache StringEscapeUtils. Поскольку я не знаю, как выглядит ваш класс клиента, я просто создаю быстрый пример, см. Классы ниже. Результат выглядит так:
soapMessage.writeTo (System.out);
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
&lt;customer&gt;
&lt;id&gt;123&amp;lt;&amp;gt;&amp;amp;g&lt;/id&gt;
&lt;name&gt;Hello&lt;/name&gt;
&lt;lastname&gt;World*+;:-&amp;lt;&amp;gt;&amp;amp;%$&lt;/lastname&gt;
&lt;/customer&gt;
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
System.out.println (StringEscapeUtils.unescapeXml (soapBody.getValue ()));
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer>
<id>123<>&g</id>
<name>Hello</name>
<lastname>World*+;:-<>&%$</lastname>
</customer>
Maven
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.6</version>
</dependency>
Main.java
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import java.io.*;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import org.apache.commons.text.StringEscapeUtils;
public class Main {
private static final Logger log = LogManager.getLogger(Main.class);
public static void main(String[] args) {
try {
Customer customer = new Customer().setID("123<>&g").setName("Hello").setLastname("World*+;:-<>&%$");
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPBody soapBody = soapEnvelope.getBody();
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(customer, sw);
soapBody.setValue(StringEscapeUtils.escapeXml11(sw.toString()));
soapMessage.saveChanges();
soapMessage.writeTo(System.out);
System.out.println("\r\n\r\n-----\r\n\r\n");
System.out.println(StringEscapeUtils.unescapeXml(soapBody.getValue()));
} catch(Exception e) {
log.error("no! ", e);
}
log.info("stop");
}
}
Customer.java
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlElement(name="id")
private String id;
@XmlElement(name="name")
private String name;
@XmlElement(name="lastname")
private String lastname;
public String getID() {
return this.id;
}
public Customer setID(String value) {
this.id = value;
return this;
}
public String getName() {
return this.name;
}
public Customer setName(String value) {
this.name = value;
return this;
}
public String getLastname() {
return this.lastname;
}
public Customer setLastname(String value) {
this.lastname = value;
return this;
}
}