0

我想获得开源 OWL-S API http://on.cs.unibas.ch/owls-api/以使用axis2。我已经设法正确发送请求,但是在响应方面我无法对其应用转换。为了让我的问题更容易回答,我提供了一些无需导入项目即可运行的独立代码。要设置 DOMSource:

String xmlString = "<ns1:countResponse xmlns:ns1=\"http://www.test.de/pill-counter\"><ns1:value>0</ns1:value><ns1:value>0</ns1:value><ns1:value>1</ns1:value><ns1:value>0</ns1:value><ns1:value>0</ns1:value><ns1:value>0</ns1:value></ns1:countResponse>";
    ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlString.getBytes());
    OMElement test = null;
    try {
        StAXBuilder builder = new StAXOMBuilder(xmlStream);
        test = (OMElement) builder.getDocument().getChildren().next();

    } catch (XMLStreamException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    OMElement documentElement = null;
      try {
        documentElement = AXIOMUtil.stringToOM(DOOMAbstractFactory.getOMFactory(), xmlString);
    } catch (XMLStreamException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

     SAAJConverterFactory convFactory = (SAAJConverterFactory) FactoryRegistry.getFactory(org.apache.axis2.jaxws.message.factory.SAAJConverterFactory.class);
     SAAJConverter conv = convFactory.getSAAJConverter();


     //Create soap 1.1 message
    SOAPMessage msg = MessageFactory.newInstance().createMessage();
    SOAPPart sp = msg.getSOAPPart();
    SOAPEnvelope se = sp.getEnvelope();
    SOAPBody soapBody = se.getBody();
    javax.xml.soap.SOAPFactory soapFactory = javax.xml.soap.SOAPFactory.newInstance();
    response = conv.toSAAJ(documentElement, soapBody, soapFactory);
    Node root = response;

现在应用转换:

Transformer transformer = null;
    try {
        transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader("<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:ns1=\"http://www.test.de/pill-counter\">\n\n\t<xsl:template match=\"/\">\n\t\t<xsl:value-of select=\"sum(*/ns1:value)\" />\n\t</xsl:template>\n</xsl:stylesheet>")));
    } catch (TransformerConfigurationException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    } catch (TransformerFactoryConfigurationError e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    try {
        transformer.transform(new DOMSource(root), new StreamResult(System.out));
    } catch (TransformerException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

运行此代码的结果是 NullPointerException。

SystemId unknown; Line num.0; Column num.0; java.lang.NullPointerException

我曾尝试在 Google、Xalan-j 邮件列表和此站点上寻找解决此问题的方法,但没有成功。我还尝试了其他几种编码方法,但没有运气。任何人的任何想法?

我找到了另一种通过从头开始生成文档来完成这项工作的方法:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setValidating(false);
    DocumentBuilder db = null;
    try {
        db = dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }          
    InputSource is = new InputSource(new StringReader(documentElement.toString()));
    Document document = null;
    try{
        document=db.parse(is);
    } catch (SAXException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    } catch (IOException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }
4

2 回答 2

0

您是否考虑过使用 wsdl2java 来构造存根,而不是直接使用低级 API?这将使您可以轻松地在 java 中操作响应。在这种情况下,使用 xslt 似乎是一种不寻常的方法。

于 2012-04-12T21:30:23.720 回答
0

要在 Axiom 树上使用 JAXP API,您无需先将其转换为 SAAJ 或 DOM。Axiom 能够创建一个可以传递给 JAXP 的 SAXSource。一个例子可以在这里找到。该示例使用javax.xml.validationAPI,但javax.xml.transform它的工作方式相同。

Note that the example uses some APIs introduced in recent Axiom versions, but the feature already exists for quite some time. Depending on the Axiom version you are using, the code needs to be adapted to the older API. In particular, instead of calling getSAXSource (which was introduced in 1.2.13), you need to construct an org.apache.axiom.om.impl.jaxp.OMSource object and pass that to JAXP.

于 2012-04-14T11:34:28.803 回答