我想获得开源 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();
}