繁体   English   中英

如何从Android中的.NET Web服务soap对象解析XML数据?

[英]How to parse XML data from .NET web service soap object in android?

我是xml概念的新手。 我从.net Web服务接收数据。 结果是此Web服务返回数据集。 我使用soap对象收到此数据集结果。 它以XML格式返回。 我无法从返回的结果中检索数据。

Web服务的输出如下所示:

 GETRESULTSResponse{GETRESULTSResult=anyType{Users=anyType{Table1=anyType{StudentID=713; RegisterNumber=2913402; StudentName=KARTHIK M; Gender=Male; CourseID=6; BranchID=27; BatchID=18; RollNumber=10SLEC603; }; }; }; }

我想获取每个元素的数据。 我不知道如何解析它。 请帮帮我。

这是我的代码段:

SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, METHOD_NAME1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
PropertyInfo pi = new PropertyInfo();
pi.setName("strSQL");
pi.setValue(ConstantValues.STUDENT_DETAILS);
pi.setType(ArrayList.class);
request.addProperty(pi);
envelope.setOutputSoapObject(request);

HttpTransportSE httpTransportSE = new HttpTransportSE(SOAP_ADDRESS);
SoapObject response = null;

httpTransportSE.call(SOAP_ACTION1, envelope);
response = (SoapObject)envelope.bodyIn;

String xml = response.toString();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);

if(totalCount > 0){
   NodeList nodes = doc.getElementsByTagName("Table1");
   for (int i = 0; i < nodes.getLength(); i++) {
  Element e = (Element)nodes.item(i);
  String studentId =  XMLfunctions.getValue(e, "StudentID");
  String regNo =  XMLfunctions.getValue(e, "RegisterNumber");
  String stuName =  XMLfunctions.getValue(e, "StudentName");
  String gender = XMLfunctions.getValue(e, "Gender");
   }
}

我试图使用此代码解析数据。 但是我无法解析它。 请提供给我一个简单的方法来解析来自.Net Webservice数据集的soap对象响应中的xml数据。

先感谢您。

最后,我找到了解决方案。

        SoapObject request = new SoapObject(ConstantValues.WSDL_TARGET_NAMESPACE, ConstantValues.METHOD_NAME1);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        PropertyInfo pi = new PropertyInfo();
        pi.setName("strSQL");
        pi.setValue(ConstantValues.STUDENT_DETAILS);
        //pi.setType(ArrayList.class);
        request.addProperty(pi);
        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransportSE = new HttpTransportSE(ConstantValues.SOAP_ADDRESS);
        SoapObject response = null;
        httpTransportSE.debug=true; 
        httpTransportSE.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

        httpTransportSE.call(ConstantValues.SOAP_ACTION1, envelope);
        response = (SoapObject)envelope.bodyIn;
        int totalCount = response.getPropertyCount();

        String resultString=httpTransportSE.responseDump;
        Log.d("XML data ",resultString);

        Document doc = XMLfunctions.XMLfromString(resultString);

        //int numResults = XMLfunctions.numResults(doc);

        System.out.println(totalCount);
        if(totalCount > 0){
            NodeList nodes = doc.getElementsByTagName("Table1");
            for (int i = 0; i < nodes.getLength(); i++) {
                studentData = new StudentDetailsData();
                Element e = (Element)nodes.item(i);

                studentData.setStudentId(Integer.parseInt(XMLfunctions.getValue(e, "StudentID")));
                studentData.setRegisterNo(XMLfunctions.getValue(e, "RegisterNumber"));
                studentData.setStudentName(XMLfunctions.getValue(e, "StudentName"));
                studentData.setGender(XMLfunctions.getValue(e, "Gender"));
                studentData.setCourseId(Integer.parseInt(XMLfunctions.getValue(e, "CourseID")));
                studentData.setBranchId(Integer.parseInt(XMLfunctions.getValue(e, "BranchID")));
                studentData.setBatchId(Integer.parseInt(XMLfunctions.getValue(e, "BatchID")));
                studentData.setRollNo(XMLfunctions.getValue(e, "RollNumber"));
                studentData.setSection(XMLfunctions.getValue(e, "Section"));

                result.add(studentData);

            }
        }

我希望这对某些人有用。 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM