如何将本地xml文件转换为org.ksoap2.serialization.SoapObject?

我正在开发android web应用程序,它需要连接web服务的响应。

我正在使用kSOAP进行Web服务调用过程。 [kSOAP是一个面向约束Java环境的SOAP Web服务客户端库,如Applets或J2ME应用程序。]

如果我将这个xml保存到本地目录, 例如。 /mnt/sdcard/appData/config.xml ,然后当我询问Web服务请求时,首先会检查本地文件是否存在,然后将该文件视为已回复的文件,否则连接到服务器。

这个过程减less了响应时间,提高了应用效率。

是否有可能将它('config.xml')转换为SOAP对象? 如何?

考虑我的XML本地文件如下:

config.xml中

<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <Response xmlns="http://testuser.com/webservices/response"> <Result> <SName>Test User</SName> <UnitDesc>SAMPLE Test </UnitDesc> <RefreshRate>60</RefreshRate> <Out> <Definition> <Code>ABC</Code> <Description>(Specific)</Description> <Action>true</Action> <Despatch>false</Despatch> </Definition> <Definition> <Code>CDE</Code><Description>(Specific)</Description> <ActionDate>true</ActionDate> </Definition> </Out> <SampleText> <string>Test XML Parsing</string> <string>Check how to convert it to SOAP response</string> <string>Try if you know</string> </SampleText> <GeneralData> <Pair> <Name>AllowRefresh</Name> <Value>Y</Value> </Pair> <Pair> <Name>ListOrder</Name> <Value>ACCENDING</Value> </Pair> </GeneralData> </Result> </Response> </soap:Body> </soap:Envelope> 

目前的代码如下所示:

 final String CONFIGURATION_FILE="config.xml"; File demoDataFile = new File("/mnt/sdcard/appData"); boolean fileAvailable=false; File[] dataFiles=demoDataFile.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String filename) { return filename.endsWith(".xml"); } }); for (File file : dataFiles) { if(file.getName().equals(CONFIGURATION_FILE)) { fileAvailable=true; } } if(fileAvailable) { //**What to do?** } else { //Create the envelope SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //Put request object into the envelope envelope.setOutputSoapObject(request); //Set other properties envelope.encodingStyle = SoapSerializationEnvelope.XSD; envelope.dotNet = true; String method="test"; synchronized (transportLockObject) { String soapAction = "http://testuser.com/webservices/response/"+method; try { transport.call(soapAction, envelope); } catch (SSLHandshakeException she) { she.printStackTrace(); SecurityService.initSSLSocketFactory(ctx); transport.call(soapAction, envelope); } } //Get the response Object response = envelope.getResponse(); //Check if response is available... if yes parse the response if (response != null) { if (sampleResponse != null) { sampleResponse.parse(response); } } else { // Throw no response exception throw new NoResponseException("No response received for " + method + " operation"); } } 

Solutions Collecting From Web of "如何将本地xml文件转换为org.ksoap2.serialization.SoapObject?"

您可以扩展HttpTransportSE类并重写像这样的方法call

 public void call(String soapAction, SoapEnvelope envelope) throws IOException, XmlPullParserException { if(localFileAvailable) { InputStream is = new FileInputStream(fileWithXml); parseResponse(envelope, is); is.close(); } else { super.call(soapAction, envelope); } } 

问题是如何将XML文件转换为SoapObject。 所以如何让你的inputxml信封进入ksoap2调用。

在HttpTransportSE类中实际上可以使用这种方法,即使这不是它的预期用途!

有一个方法“parseResponse”接受信封和inputstream(您的XML文件),并更新信封input标题和正文。 但巧妙的是,你可以将这些复制到outHeader和outBody字段中,然后映射字段的所有努力都会消失。

  @Override public void call(String soapAction, SoapEnvelope envelope) throws IOException, XmlPullParserException { if ( getFileInputStream() != null ){ parseResponse(envelope, getFileInputStream()); envelope.bodyOut = envelope.bodyIn; envelope.headerOut = envelope.headerIn; getFileInputStream().close(); } super.call(soapAction,envelope); }