gpt4 book ai didi

java - 在 Java 中保存 .xml 文件

转载 作者:行者123 更新时间:2023-12-01 05:04:01 25 4
gpt4 key购买 nike

我正在使用 WebServiceTemplate 类,并且将从 Web 服务收到的 XML 响应存储在文件中。但由于某种原因,.xml 文件包含所有 < , >另存为;&lt;&gt

有什么方法可以将其转换为正确的 .xml文件保存之前?

        InputStream inputStream = new   WebServiceClientImpl().getClass().getResourceAsStream("Request.xml");
StreamSource source = new StreamSource(inputStream2);
StreamResult result = new StreamResult(new File("Response.xml"));
webServiceTemplate.sendSourceAndReceiveToResult(defaultURI,source, result);

请求.xml:

 <list>
<requestXmlString xs:type="type:string" xmlns:xs="http://www.w3.org/2000/XMLSchema- instance">
<![CDATA[<request operationName="list" locale="en">
<resourceDescriptor name="" wsType="folder" uriString="/" isNew="false">
<label>null</label>
</resourceDescriptor>
</request>]]>
</requestXmlString>
</list>

响应.xml

  <?xml version="1.0" encoding="UTF-8"?><listResponse   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"   soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><listReturn   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;operationResult version="2.0.1"&gt;
&lt;returnCode&gt;&lt;![CDATA[0]]&gt;&lt;/returnCode&gt;
&lt;resourceDescriptor name="Record1" wsType="reportUnit" uriString="/xyz" isNew="false"&gt;
&lt;label&gt;&lt;![RECORD 1]]&gt;&lt;/label&gt;
&lt;creationDate&gt;1338285680000&lt;/creationDate&gt;
&lt;resourceProperty name="PROP_RESOURCE_TYPE"&gt;
&lt;value&gt;&lt;!

最佳答案

写入文件时,< > & 等字符会被序列化,以便 XML 解析器稍后可以读取它们。

您可以尝试在文本节点上使用 CDATA 编码。在这种情况下,CDATA block 中将包含 <。

http://en.wikipedia.org/wiki/CDATA

关于java - 在 Java 中保存 .xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12970202/

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