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

时间:2011-10-21 12:54:46

标签: java xml web-services

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

请帮忙。

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

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

************第一个答案的后续问题在这里:
How to return arbitrary XML Document using an Eclipse/AXIS2 POJO Service

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

这是一个示例回复

<?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>

1 个答案:

答案 0 :(得分:7)

框架正在做你所说的;您的方法返回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文档”返回类型或注释,可以实现相同的效果。