使用sax解析xml响应

问题描述:

我一直在关注使用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响应值。

我有固定的代码,你可以进行如下操作:

import java.io.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import org.xml.sax.*; 
import org.xml.sax.helpers.*; 

/** 
* Demo xml processing 
*/ 
public class Demo { 

    private static final Logger log = Logger.getLogger(Demo.class.getName()); 

    private static final int CHUNK = 1048576; //1MB chunk of file 

    public static void main(String[] args) { 

     try { 
      ByteArrayOutputStream out = new ByteArrayOutputStream(CHUNK); 

      Writer writer = new OutputStreamWriter(out, "UTF-8"); 

      /* here put soapMessage.writeTo(out); 
       I will just process this hard-coded xml */ 
      writer.append("<greeting>Hello!</greeting>"); 
      writer.flush(); 

      ByteArrayInputStream is 
        = new ByteArrayInputStream(out.toByteArray()); 

      XMLReader reader = XMLReaderFactory.createXMLReader(); 

      //define your handler which extends default handler somewhere else 
      MyHandler handler = new MyHandler(); 
      reader.setContentHandler(handler); 

      /* reader will be closed with input stream */ 
      reader.parse(new InputSource(new InputStreamReader(is, "UTF-8"))); 
      //Hello in the console 
     } catch (UnsupportedEncodingException ex) { 
      log.severe("Unsupported encoding"); 
     } catch (IOException | SAXException ex) { 
      log.severe("Parsing error!"); 
     } finally { 
      /*everything is ok with byte array streams! 
       closing them has no effect! */ 
     } 

    } 
} 

class MyHandler extends DefaultHandler { 

    @Override 
    public void characters(char ch[], int start, int length) 
      throws SAXException { 
     System.out.print(String.copyValueOf(ch, start, length)); 
    } 
} 
+0

感谢您寻找到我的问题。我能够传递肥皂响应,但是我的代码在某处被挂起。你可以阅读下面的帖子知道我的问题http://*.com/questions/42210300/stream-soap-response-and-parse-using-sax-parser – user4324324

+0

哎呀,似乎我草绘了代码,但省略了流从酿造关闭,我会修改帖子(不要抛出异常,但抓住它们并关闭流,因为它应该完成)。那么你的'挂'可能会消失 –

+0

刚刚发现我欺骗了自己,更糟糕的是,你也看到这个:http://*.com/questions/484119/why-doesnt-more-java-code-使用pipedinputstream pipedoutputstream –