将xalan java转换应用于axis2对象?

时间:2012-04-12 15:13:58

标签: java axis2 xalan

我想要使用开源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();
    }

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

请注意,该示例使用了最近Axiom版本中引入的一些API,但该功能已存在很长时间了。根据您使用的Axiom版本,代码需要适应旧的API。特别是,您需要构造一个getSAXSource对象并将其传递给JAXP,而不是调用org.apache.axiom.om.impl.jaxp.OMSource(在1.2.13中引入)。