java字符串返回不了吗_Java Web Service返回带有>和<而不是>和<的字符串

我有一个返回字符串的Java Web服务。我正在使用DocumentBuilderand

Document类创建此xml字符串的正文。当我查看返回的XML的源(在浏览器窗口中看起来不错)而不是<>时,它将返回<并>围绕XML节点。

请帮忙。

** UPDATE(包括代码示例)

该代码不包含任何错误捕获,为简单起见,将其剥离。其中包括一个代码块和三种方法:第一个代码块(示例设置)显示了设置Document对象的基本概念。该方法appendPayment(...)是实际构建文档的地方。它调用了两个辅助方法,getTagValue(...)并且prepareElement(...)

**注意,该代码用于从预先存在的xml字符串中复制特定部分xmlString,并获取必要的信息以供日后返回。

** UPDATE 2 在问题末尾添加了回复

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

这是一个示例响应

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值