如何将本地xml文件转换为org.ksoap2.serialization.SoapObject?
2022-09-04 21:57:02
我正在开发Android Web应用程序,它需要连接Web - 服务以进行响应。
我正在使用kSOAP进行Web服务调用过程。[kSOAP 是一个 SOAP Web 服务客户端库,用于受约束的 Java 环境,如 Applets 或 J2ME 应用程序。
如果我正在将响应的xml保存到本地目录中,例如/mnt/sdcard/appData/config.xml然后当我请求Web服务时,它首先会检查本地文件是否存在,然后将该文件视为响应文件,否则将连接到服务器。
此过程可缩短响应时间并提高应用程序效率。
是否可以将其(“配置.xml”)转换为 SOAP 对象?又是如何做到的呢?
考虑我的xml本地文件如下:
配置.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");
}
}