0
Я читал this учебник, чтобы использовать парсер сакса. Если мой ввод использует xml-файл, то нижняя строка работает нормально. Но как я могу разобрать xml, который я получаю в ответ от веб-службы. Как передать мыльный отклик в качестве входного сигнала для парсера саксов?Parse xml response with sax
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
oops похоже, что я набросал код, но пропущенный поток закрытие от ярости, я изменю сообщение (не бросать исключения, а ловить их и закрывать потоки, как это и должно быть сделано). Тогда ваши «зависания», вероятно, исчезнут –
только что обнаружили, что я обманул себя и, что еще хуже, вы тоже увидите это: http://stackoverflow.com/questions/484119/why-doesnt-more-java-code- use-pipedinputstream-pipedoutputstream –