我有一个返回字符串的Java Web服务。我正在使用DocumentBuilder和Document类创建此xml字符串的主体。当我查看返回的XML的源代码(在浏览器窗口中看起来不错)而不是时,它将在XML节点周围返回&lt;和&gt;。请帮忙。**** UPDATE(包括代码示例)该代码不包含任何错误捕获,为简单起见将其删除。其中包括一个代码块和三种方法:第一个代码块(EXAMPLE SETUP)显示了设置Document对象的基本概念。 appendPayment(...)方法是实际构建文档的地方。它调用两种帮助方法getTagValue(...)和prepareElement(...)**注意,此代码用于从预先存在的xml字符串xmlString复制特定部分,并获取必要的信息以供日后返回。****更新2问题末尾添加了答复************第一个答案的后续问题在这里: How to return arbitrary XML Document using an Eclipse/AXIS2 POJO ServiceEXAMPLE 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对象转换为Stringpublic 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.1Content-Type: text/xml;charset=utf-8Transfer-Encoding: chunked这是一个示例响应<?xml version="1.0" encoding="UTF-8" standalone="no"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <getTransactionsResponse xmlns="http://services.paypal.com"> <getTransactionsReturn>&lt;AllTransactions&gt;&lt;Transaction&gt;&lt;status&gt;PENDING&lt;/status&gt;&lt;amount&gt;55.55&lt;/amount&gt;&lt;/transaction&gt; </getTransactionsResponse> </soapenv:Body></soapenv:Envelope> (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 框架正在按照您所说的去做。您的方法返回一个String,这意味着生成的WSDL应该具有类型 <xsd:string> 的响应消息。众所周知,XML字符串必须将某些字符编码为character entity references(即“<”变为“&lt;”,因此XML解析器将其视为字符串,而不是您期望的XML元素的开头)。如果要返回XML文档,则必须在WSDL <types> section中定义XML结构,并将响应消息部分设置为适当的元素。换句话说,您尝试不使用SOAP / WSDL提供的强类型系统(即XML模式)来发送“类型化”数据;这通常被认为是不良设计(请参阅Loosely typed versus strongly typed web services)。最终的解决方案是通过适当的XML Schema定义响应文档。如果没有设置架构(如服务设计那样),则将 <xsd:any> 类型用作消息响应类型,尽管这种方法为has its pitfalls。此外,这样的重新设计意味着模式优先(自上而下)的开发模型,并且从注释流中看来,您当前正在实践代码优先(自下而上)的方法。也许您的工具提供了实现相同效果的机制,例如“通用XML文档”返回类型或注释。 (adsbygoogle = window.adsbygoogle || []).push({});
07-24 21:48