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

标签 java android xml xml-parsing xml-serialization

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

我正在使用 kSOAP 进行网络服务调用过程。 [kSOAP 是一个 SOAP Web 服务客户端库,用于受限的 Java 环境,例如 Applets 或 J2ME 应用程序。]

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

这个过程减少了响应时间并提高了应用程序的效率。

是否可以将它('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");
        }

}

最佳答案

您可以扩展 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);
    }
}

关于java - 如何将本地 xml 文件转换为 org.ksoap2.serialization.SoapObject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17920015/

相关文章:

java - Selenium 无法识别 IE 中新打开的浏览器上的元素

Java:将无符号 8 字节时间戳转换为微秒精确日期

java - finish返回到哪个方法?

xml - 如何修复损坏的 csproj 文件?

Java 2D PNG : Calculate Dimension

java - 沿着 LibGDX 中的路径 vector 偏移 Sprite

android - 使 Android 消息应用程序识别从其他应用程序发送的消息

java - 在多个内部循环中使用大量内存

c# - Xml - '=' 是意外 token ,预期 token 是 ';'

swift - 我是 swift 的新手。我在 xml 解析完成后创建了表格 View 。它只显示单元格的高度。空白单元格