Java Web Service返回带有&gt;的字符串 和&lt; 而不是>和<

我有一个返回字符串的java Web服务。 我正在使用DocumentBuilderDocument类创建此xml字符串的主体。 当我查看返回的XML的源代码(在浏览器窗口中看起来很好)而不是它返回<> 围绕XML节点。

请帮忙。

****更新(包括代码示例)
代码不包括任何错误捕获,为简单起见,它被剥离。 包含一个代码块和三个方法:第一个代码块(示例设置)显示了Document对象设置的基础知识。 方法appendPayment(...)是实际文档构建发生的地方。 它调用两个辅助方法getTagValue(...)prepareElement(...)
**注意,此代码用于从预先存在的xml字符串xmlString复制特定部分,并获取稍后要返回的必要信息。

****更新2在问题结尾处添加了回复

************第一个答案的后续问题在这里:
如何使用Eclipse / AXIS2 POJO服务返回任意XML文档

 EXAMPLE SETUP { //create new document DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder newDocBuilder = docFactory.newDocumentBuilder(); Document newDoc = newDocBuilder.newDocument(); Element rootElement = newDoc.createElement("AllTransactions"); newDoc.appendChild(rootElement); appendPayment(stringXML, newDoc); } public static void appendPayment(String xmlString, Document newDoc) throws Exception { //convert string to inputstream ByteArrayInputStream bais = new ByteArrayInputStream(xmlString.getBytes()); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document oldDoc = docBuilder.parse(bais); oldDoc.getDocumentElement().normalize(); NodeList nList = oldDoc.getChildNodes(); Node nNode = nList.item(0); Element eElement = (Element) nNode; //Create new child node for this payment Element transaction = newDoc.createElement("Transaction"); newDoc.getDocumentElement().appendChild(transaction); //status transaction.appendChild(prepareElement("status", eElement, newDoc)); //amount transaction.appendChild(prepareElement("amount", eElement, newDoc)); } private static String getTagValue(String sTag, Element eElement) { NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes(); Node nValue = (Node) nlList.item(0); return nValue.getNodeValue(); } private static Element prepareElement(String sTag, Element eElement, Document newDoc) { String str = getTagValue(sTag, eElement); Element newElement = newDoc.createElement(sTag); newElement.appendChild(newDoc.createTextNode(str)); return newElement; } 

最后,我使用以下方法将最终的Document对象转换为String

 public static String getStringFromDocument(Document doc) { try { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); } catch(TransformerException ex) { ex.printStackTrace(); return null; } } 

响应的头类型如下

 Server: Apache-Coyote/1.1 Content-Type: text/xml;charset=utf-8 Transfer-Encoding: chunked 

这是一个示例响应

     <AllTransactions><Transaction><status>PENDING</status><amount>55.55</amount></transaction>    

框架正在做你所说的; 您的方法返回一个String ,这意味着生成的WSDL应该有一个类型为的响应消息。 我们知道,XML字符串必须将某些字符编码为字符实体引用 (即“ < ”变为“ < ”因此XML解析器将其视为字符串,而不是您想要的XML元素的开头)。 如果要返回XML文档,则必须在WSDL 部分中定义XML结构,并将响应消息部分设置为相应的元素。

换句话说,您尝试发送“类型化”数据而不使用SOAP / WSDL提供的强类型系统(即XML模式); 这通常被视为糟糕的设计(请参阅松散类型与强类型Web服务 )。

最终的解决方案是通过适当的XML Schema定义响应文档。 如果没有设置模式,例如服务的设计,那么使用类型作为消息响应类型,尽管这种方法有其缺陷 。 此外,这样的重新设计意味着一个模式优先(自上而下)的开发模型,从评论流看来,您似乎正在实践代码优先(自下而上)方法。 也许您的工具提供了一种机制,例如“常规XML文档”返回类型或注释,它们可以实现相同的效果。