Java Web Service返回带有& gt;的字符串.和& lt;而不是>和< [英] Java Web Service returns string with &gt; and &lt; instead of > and <
问题描述
我有一个返回字符串的Java Web服务.我正在使用 DocumentBuilder
和 Document
类创建此xml字符串的主体.当我查看返回的XML的源代码(在浏览器窗口中看起来不错)而不是<>时,它将围绕XML返回& lt
和& gt
节点.
I have a java web service that returns a string. I'm creating the body of this xml string with a DocumentBuilder
and Document
class. When I view source of the returned XML (Which looks fine in the browser window) instead of <> it's returning <
and >
around the XML nodes.
请帮助.
**** UPDATE(包括代码示例)
该代码不包含任何错误捕获,为简单起见将其删除.其中包括一个代码块和三种方法:第一个代码块(示例设置)显示了设置Document对象的基本概念.方法 appendPayment(...)
是实际构建文档的地方.它调用两个帮助器方法 getTagValue(...)
和 prepareElement(...)
**注意,此代码旨在从先前存在的xml字符串 xmlString
复制特定部分,并获取必要的信息以供日后返回.
****UPDATE (including code example)
The code is not including any error catching, it was stripped for simplicity.
One code block and three methods are included:
The first code block (EXAMPLE SETUP) shows the basics of what the Document object is setup to be like. the method appendPayment(...)
is where the actual document building happens. It calls on two helper methods getTagValue(...)
and prepareElement(...)
**Note, this code is meant to copy specific parts from a pre-existing xml string, xmlString
, and grap the necessary information to be returned later.
****更新2 在问题末尾添加了答案
****UPDATE 2 Response added at the end of the question
************第一个答案的后续问题在这里:
如何使用Eclipse/AXIS2 POJO服务
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
Finally, I use the following method to convert the final Document
object to a 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
这是一个示例响应
<?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><AllTransactions><Transaction><status>PENDING</status><amount>55.55</amount></transaction>
</getTransactionsResponse>
</soapenv:Body>
</soapenv:Envelope>
推荐答案
框架正在按照您所说的进行;您的方法返回 String
,这意味着生成的WSDL应该具有类型为 < xsd:string>
.众所周知,XML字符串必须将某些字符编码为字符实体引用(即"<
变为" & lt;
,因此XML解析器将其视为字符串,而不是您期望的XML元素的开头).如果要返回XML文档,则必须在 WSDL <中定义XML结构.类型>
部分,然后将响应消息部分设置为适当的元素.
The framework is doing what you tell it; your method returns a String
which means the generated WSDL should have a response message of type <xsd:string>
. As we know, XML strings must encode certain characters as character entity references (i.e. "<
" becomes "<
" so the XML parser treats it as a string, not the beginning of an XML element as you intend). If you want to return an XML document then you must define the XML structure in the WSDL <types>
section and set the response message part to the appropriate element.
换句话说,您尝试不使用SOAP/WSDL提供的强类型系统(即XML模式)来发送类型化"数据;这通常被认为是错误的设计(请参见 强类型的Web服务 ).
To put it another way, you are trying to send "typed" data without using the strong type system provided by SOAP/WSDL (namely XML schema); this is generally regarded as bad design (see Loosely typed versus strongly typed web services).
最终的解决方案是通过适当的 XML架构定义响应文档.如果没有设置的架构(如服务设计那样),请使用有其陷阱.此外,这样的重新设计意味着模式优先(自上而下)的开发模型,并且从注释流中看来,您当前正在实践代码优先(自下而上)的方法.也许您的工具提供了一种可以达到相同效果的机制,例如通用XML文档"返回类型或注释.
The ultimate solution is to to define the response document via a proper XML Schema. If there is no set schema, as by the design of your service, then use the <xsd:any>
type for the message response type, although this approach has its pitfalls. Moreover, such a redesign implies a schema-first (top-down) development model and from the comment stream it seems that you are currently practicing code-first (bottom-up) approach. Perhaps your tools provide a mechanism such as a "general XML document" return type or annotation which achieves the same effect.
这篇关于Java Web Service返回带有& gt;的字符串.和& lt;而不是>和<的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!