gpt4 book ai didi

java - 无法通过 Web 服务方法返回 org.w3c.dom.Document

转载 作者:行者123 更新时间:2023-12-01 04:52:41 24 4
gpt4 key购买 nike

我正在尝试从 java axis2 Web 服务返回 XML 文档对象。当我尝试在客户端获取 Document 对象时,它给了我这些异常。

org.apache.axis2.AxisFault: org.apache.axis2.AxisFault: Mapping qname not fond for the package: com.sun.org.apache.xerces.internal.dom
at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:531)
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:375)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:421)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
at com.turnkey.DataCollectorStub.getData(DataCollectorStub.java:194)
at com.turnkey.TestClient.main(TestClient.java:28)

我可以不从 Web 服务返回 Document 对象吗?不过,该服务确实返回 XML 字符串。

下面是我正在使用的方法的伪代码

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public Document getData(args)
{
String xmlSource = "/*XML string*/";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document xmlDoc = builder.parse(new InputSource(new StringReader(xmlSource)));
return xmlDoc;
}

顺便说一句,这个方法在服务器端工作正常,但在客户端我无法接收Document对象

任何人都可以帮助我吗?

最佳答案

简单的方法不使用Document作为返回值,因为axis2在架构中找不到合适的导入。如果每次都生成 wsdl,则应该将 import org.w3c.dom.Document 添加到 wsdl 模式(这是一个不方便的解决方案)。这就是为什么在我看来最好的方法是返回特定实体

 public Credit[] getCreditList(){
Credit[] credits = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document xmlDoc = documentBuilder.parse(XML_REAL_PATH);

Element root = xmlDoc.getDocumentElement();

List<Credit> creditsList = new ArrayList<>();

NodeList creditNodes = root.getElementsByTagName(CREDIT);
int countCreditNodes = creditNodes.getLength();

for (int i = 0; i < countCreditNodes; i++) {
Element creditElement = (Element) creditNodes.item(i);

Credit credit = new Credit();

Element child = (Element) creditElement.getElementsByTagName(OWNER).item(0);
String owner = child.getFirstChild().getNodeValue();
credit.setOwner(owner);

//...
creditsList.add(credit);
}
credits = creditsList.toArray(new Credit[creditsList.size()]);

} catch (SAXException | IOException | ParserConfigurationException ex) {
Logger.getLogger(CreditPayService.class.getName()).log(Level.SEVERE, null, ex);
}
return credits;
}

关于java - 无法通过 Web 服务方法返回 org.w3c.dom.Document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14703825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com