使用sax解析xml响应

时间:2017-02-11 14:12:35

标签: java xml jaxb sax saxparser

我一直在关注this教程以使用sax解析器。如果我的输入是使用xml文件,那么下面的行正常工作。但是,如何解析我从Web服务获得的响应中获取的xml。如何将soap响应作为输入传递给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响应中解析并获取值。

1 个答案:

答案 0 :(得分:1)

我修复了代码,您可以按照以下步骤操作:

$request = new \Facebook\FacebookRequest(
    $fbApp,
    $accessToken,
   'POST',
   '/<facebook-page-id>/feed',
    [
        'message' => 'With Geolocate Ib',
        'targeting' => "{'geo_locations': {'cities': [{'key':" . <city key> . "}]}}"
    ]
);