Sab*_*ado 5 java xml web-services
我有一个返回字符串的java Web服务.我用a DocumentBuilder和Documentclass 创建了这个xml字符串的主体.当我查看返回的XML的源代码(在浏览器窗口中看起来很好)而不是<>它返回<并>围绕XML节点.
请帮忙.
****UPDATE(包括代码示例)
代码不包括任何错误捕获,为简单起见,它被剥离.包含一个代码块和三个方法:第一个代码块(示例设置)显示了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;
}
Run Code Online (Sandbox Code Playgroud)
最后,我使用以下方法将最终Document对象转换为aString
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;
}
}
Run Code Online (Sandbox Code Playgroud)
响应的头类型如下
Server: Apache-Coyote/1.1
Content-Type: text/xml;charset=utf-8
Transfer-Encoding: chunked
Run Code Online (Sandbox Code Playgroud)
这是一个示例响应
<?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>
Run Code Online (Sandbox Code Playgroud)
框架正在做你所说的; 你的方法返回一个String意味着生成的WSDL应该有一个类型的响应消息<xsd:string>.我们知道,XML字符串必须将某些字符编码为字符实体引用(即" <"变为" <",因此XML解析器将其视为字符串,而不是您想要的XML元素的开头).如果要返回XML文档,则必须在WSDL <types>部分中定义XML结构,并将响应消息部分设置为相应的元素.
换句话说,您尝试发送"类型化"数据而不使用SOAP/WSDL提供的强类型系统(即XML模式); 这通常被视为糟糕的设计(请参阅松散类型与强类型的Web服务).
最终的解决方案是通过适当的XML Schema定义响应文档.如果没有设置模式,例如服务的设计,那么使用<xsd:any>消息响应类型的类型,尽管这种方法有其缺陷.此外,这样的重新设计意味着模式优先(自上而下)开发模型,并且从评论流看来,您似乎正在实践代码优先(自下而上)方法.也许您的工具提供了一种机制,例如"常规XML文档"返回类型或注释,它们可以实现相同的效果.
| 归档时间: |
|
| 查看次数: |
13721 次 |
| 最近记录: |