gpt4 book ai didi

java - SOAPMessage 如何解析<![CDATA[ ]]>?

转载 作者:行者123 更新时间:2023-12-02 04:34:50 24 4
gpt4 key购买 nike

下面是将 java 对象编码到 SOAPMessage 中的代码:

     public static SOAPMessage encode(String key,Object object) throws JAXBException, SOAPException{
JAXBContext airContext = newInstance("com.test");
contextMap.put("ws", airContext);
Marshaller marshaller = airContext .createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
marshaller.marshal(object, message.getSOAPBody());
message.saveChanges();
return message;
}

对象喜欢:

 @XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
private String name;
private String surname;
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}
}

我想将其编码到此 xml:

<root>
<name><![CDATA[test]]></name>
<surname>test</surname>
</root>

但现在它显示为:

<root>
<name>&lt;![CDATA[test]]&gt;</name>
<surname>test</surname>
</root>

你能告诉我这是怎么回事吗?

最佳答案

您无法直接解析 CDATA 内的 xml,因为添加为 CDATA 的全部目的是被解析器忽略。您可以如下获取 CDATA 元素中的数据并可以对其进行解析。

SOAPBody soapBody = soapMessage.getSOAPBody();
NodeList nodeList = soapBody.getElementsByTagName("outerElementOfCDATA");
Element element = (Element) nodeList.item(0);
Node child = element.getFirstChild();
String characterData;
if (child instanceof CharacterData) {
characterData = ((CharacterData) child).getData();
}

关于java - SOAPMessage 如何解析&lt;![CDATA[ ]]>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30971116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com