【问题标题】:Cannot return an org.w3c.dom.Document by web-service method无法通过 Web 服务方法返回 org.w3c.dom.Document
【发布时间】:2013-01-20 03:51:55
【问题描述】:

我正在尝试从 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 对象

谁能帮帮我。

【问题讨论】:

    标签: java xml web-services axis2


    【解决方案1】:

    简单的方法不使用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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 2010-09-05
      • 1970-01-01
      • 2015-05-20
      相关资源
      最近更新 更多