Java SOAP请求XML中的CDATA节
我使用的是JAX-WS服务。下面是request类的一部分Java SOAP请求XML中的CDATA节,java,xml,jax-ws,marshalling,cdata,Java,Xml,Jax Ws,Marshalling,Cdata,我使用的是JAX-WS服务。下面是request类的一部分 @XmlElement(name = "Answers") protected String answers; 现在,在实际的SOAP请求中,答案需要以CDATA格式在xml中发送。答案有一个单独的存根类。因此,我将该类的对象编组为xml。我在CDATA标记中对此进行了讨论,如下所示: xmlstr = "<![CDATA[" + xmlstr + "]]>"; xmlstr=”“; 因此,我的请求xml应该如下所
@XmlElement(name = "Answers")
protected String answers;
现在,在实际的SOAP请求中,答案需要以CDATA格式在xml中发送。答案有一个单独的存根类。因此,我将该类的对象编组为xml。我在CDATA标记中对此进行了讨论,如下所示:
xmlstr = "<![CDATA[" + xmlstr + "]]>";
xmlstr=”“;
因此,我的请求xml应该如下所示:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<!-- Other tags -->
<Answers>
<![CDATA[
<TagOne></TagOne>
<TagTwo></TagTwo>
]]>
</Answers>
</S:Body>
</S:Envelope>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<!-- Other tags -->
<Answers>
<![CDATA[
<TagOne></TagOne>
<TagTwo></TagTwo>
]]>
</Answers>
</S:Body>
</S:Envelope>
]]>
但是,当请求从SOAPLoggingHandler发送到服务器时,它看起来是这样的:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<!-- Other tags -->
<Answers>
<![CDATA[
<TagOne></TagOne>
<TagTwo></TagTwo>
]]>
</Answers>
</S:Body>
</S:Envelope>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<!-- Other tags -->
<Answers>
<![CDATA[
<TagOne></TagOne>
<TagTwo></TagTwo>
]]>
</Answers>
</S:Body>
</S:Envelope>
![CDATA[
TagOne>/TagOne
;
TagTwo>/TagTwo
;
]]
由于这种字符转义,我收到的响应是“无效答案xml格式”。我有两个问题:
如果还需要什么,请告诉我。我认为这里的情况是您的java对象表示与您希望在XML中看到的不太匹配。假设您将JAXB与JAX-WS结合使用,那么您可以使用XmlJavaTypeAdapter注释为bean中的元素创建适配器类
public class CDATAAdapter extends XmlAdapter<String, String> {
@Override
public String marshal(String v) throws Exception {
return "<![CDATA[" + v + "]]>";
}
@Override
public String unmarshal(String v) throws Exception {
return v;
}
}
有关更多详细信息,请阅读我认为这里的情况是您的java对象表示与您希望在XML中看到的不太匹配。假设您将JAXB与JAX-WS结合使用,那么您可以使用XmlJavaTypeAdapter注释为bean中的元素创建适配器类
public class CDATAAdapter extends XmlAdapter<String, String> {
@Override
public String marshal(String v) throws Exception {
return "<![CDATA[" + v + "]]>";
}
@Override
public String unmarshal(String v) throws Exception {
return v;
}
}
有关更多详细信息,请阅读为什么需要将其作为CDATA部分发送?数据在XML中的表示方式应该无关紧要(在CDATA中或使用实体转义的特殊字符的普通格式中)。为什么它需要作为CDATA节发送?数据在XML中的表示方式应该无关紧要(在CDATA中或用实体转义的特殊字符表示的普通数据)。可能的重复数据在CDATA中不起作用。结果是一样的![CDATA[与CDATA不兼容。结果与![CDATA相同[