0
我一直在關注使用sax解析器的this教程。如果我的輸入是使用xml文件,那麼下面的行工作正常。但我怎樣才能解析XML,我從Web服務得到的響應。如何將soap響應作爲輸入傳遞給sax解析器?使用sax解析xml響應
new MySaxParser("catalog.xml");
我的代碼
public class soapTest{
private static SOAPMessage createSoapRequest() throws Exception{
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
soapEnvelope.addNamespaceDeclaration("action", "http://www.webserviceX.NET/");
SOAPBody soapBody = soapEnvelope.getBody();
SOAPElement soapElement = soapBody.addChildElement("GetQuote", "action");
SOAPElement element1 = soapElement.addChildElement("symbol", "action");
element1.addTextNode("ticket");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", "http://www.webserviceX.NET/GetQuote");
soapMessage.saveChanges();
System.out.println("----------SOAP Request------------");
soapMessage.writeTo(System.out);
return soapMessage;
}
private static void createSoapResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.println("\n----------SOAP Response-----------");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
public static void main(String args[]){
try{
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
String url = "http://www.webservicex.net/stockquote.asmx?wsdl";
SOAPMessage soapRequest = createSoapRequest();
//hit soapRequest to the server to get response
SOAPMessage soapResponse = soapConnection.call(soapRequest, url);
// Not able to proceed from here. How to use sax parser here
soapConnection.close();
}catch (Exception e) {
e.printStackTrace();
}
}
如何解析,並從XML響應值。
感謝您尋找到我的問題。我能夠傳遞肥皂響應,但是我的代碼在某處被掛起。你可以閱讀下面的帖子知道我的問題http://stackoverflow.com/questions/42210300/stream-soap-response-and-parse-using-sax-parser – user4324324
哎呀,似乎我草繪了代碼,但省略了流從釀造關閉,我會修改帖子(不要拋出異常,但抓住它們並關閉流,因爲它應該完成)。那麼你的'掛'可能會消失 –
剛剛發現我欺騙了自己,更糟糕的是,你也看到這個:http://stackoverflow.com/questions/484119/why-doesnt-more-java-code-使用pipedinputstream pipedoutputstream –