gpt4 book ai didi

java - 使用 Java 将包含 XML 的字符串添加到 SOAPElement

转载 作者:数据小太阳 更新时间:2023-10-29 02:28:57 26 4
gpt4 key购买 nike

我必须将 excel 文件中的数据作为 XML 发送到网络服务。表中的数据看起来有点像这样: see example of table here!

第一行始终包含列数据的 XML 标记。大多数数据列只包含字符串,但有些包含 xml。这些子节点对于 Web 服务接受数据很重要。

我正在使用 SOAP,为每一列创建一个新的 SOAPElement,其中每一行都是一个新的 SOAP 请求。

SOAPElement newElement = body.addChildElement(tagForThisColumn);
newElement.addTextNode(stringValueOfCell);

这非常适用于字符串值,但 SOAPElement 使用 xml 转义单元格的所有“<”和“>”。

我已经搜索了答案并找到了类似问题的一些解决方案,但没有一个适合我的。

最佳答案

您应该向 SoapElement 添加一个节点。我通过将 SAML token 添加到 soap header 中的 SoapElement 来完成类似的操作:

SOAPHeader header = envelope.addHeader();

SOAPElement security = header.addChildElement("Security", "wsse",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

documentBuilderFactory.setNamespaceAware(true);

DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
File file = new File("your-file.xml");

/* parse existing file to DOM */
Document document = documentBuilder.parse(new FileInputStream(file));

Document securityDoc = security.getOwnerDocument();
Node newNode = securityDoc.importNode(document.getFirstChild(), true);

//Add the Node
security.appendChild(newNode);

希望有用。

关于java - 使用 Java 将包含 XML 的字符串添加到 SOAPElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40041430/

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