使用xml解析器方法解析xml soap响应

时间:2015-04-03 13:01:12

标签: android xml web-services soap wsdl

我有一个xml格式的soap响应我想用任何解析方法解析它(我不使用soap解析方法)。 以下是响应字符串。

<?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>

<GetManufactureAndTypeResponse xmlns="http://tempuri.org/"><GetManufactureAndTypeResult><xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Manufacture">
<xs:complexType>
<xs:sequence>
<xs:element name="ManufacturerId" type="xs:int" minOccurs="0" />
<xs:element name="ManufactureName" type="xs:string" minOccurs="0" />
<xs:element name="SalesOfficeId" type="xs:int" minOccurs="0" />
<xs:element name="Address" type="xs:string" minOccurs="0" /><xs:element name="Suite" type="xs:string" minOccurs="0" /><xs:element name="City" type="xs:string" minOccurs="0" /><xs:element name="State" type="xs:string" minOccurs="0" /><xs:element name="Country" type="xs:string" minOccurs="0" /><xs:element name="Zip" type="xs:string" minOccurs="0" /><xs:element name="Website" type="xs:string" minOccurs="0" /><xs:element name="SalesRepName" type="xs:string" minOccurs="0" /><xs:element name="SalesRepContactNo" type="xs:string" minOccurs="0" /><xs:element name="SalesRepExt" type="xs:string" minOccurs="0" /><xs:element name="SalesRepCellNo" type="xs:string" minOccurs="0" /><xs:element name="SalesRepEmail" type="xs:string" minOccurs="0" /><xs:element name="Active" type="xs:boolean" minOccurs="0" /><xs:element name="ModifiedDate" type="xs:dateTime" minOccurs="0" /><xs:element name="ModifiedUserId" type="xs:int" minOccurs="0" /><xs:element name="CreatedDate" type="xs:dateTime" minOccurs="0" /><xs:element name="CreatedUserId" type="xs:int" minOccurs="0" /></xs:sequence></xs:complexType></xs:element><xs:element name="Type"><xs:complexType><xs:sequence><xs:element name="ItemType" type="xs:string" minOccurs="0" /><xs:element name="typ" type="xs:string" minOccurs="0" /></xs:sequence></xs:complexType></xs:element><xs:element name="TypePopUp"><xs:complexType><xs:sequence><xs:element name="ItemType" type="xs:string" minOccurs="0" /><xs:element name="typ" type="xs:string" minOccurs="0" /></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><NewDataSet xmlns=""><Manufacture diffgr:id="Manufacture1" msdata:rowOrder="0"><ManufacturerId>1261</ManufacturerId><ManufactureName>IKO</ManufactureName><SalesOfficeId>441</SalesOfficeId><Address /><City /><State /><Zip /><Active>true</Active><ModifiedDate>2014-10-13T03:13:21.807-04:00</ModifiedDate><ModifiedUserId>1501</ModifiedUserId><CreatedDate>2014-10-13T03:13:21.807-04:00</CreatedDate><CreatedUserId>1501</CreatedUserId></Manufacture><Manufacture diffgr:id="Manufacture2" msdata:rowOrder="1"><ManufacturerId>1262</ManufacturerId><ManufactureName>Gaf</ManufactureName><SalesOfficeId>441</SalesOfficeId><Address /><City /><State /><Zip /><Active>true</Active><ModifiedDate>2014-10-13T03:13:21.82-04:00</ModifiedDate><ModifiedUserId>1501</ModifiedUserId><CreatedDate>2014-10-13T03:13:21.82-04:00</CreatedDate><CreatedUserId>1501</CreatedUserId></Manufacture><Manufacture diffgr:id="Manufacture3" msdata:rowOrder="2"><ManufacturerId>1263</ManufacturerId><ManufactureName>OC</ManufactureName><SalesOfficeId>441</SalesOfficeId><Address /><City /><State /><Zip /><Active>true</Active><ModifiedDate>2014-10-13T03:13:21.823-04:00</ModifiedDate><ModifiedUserId>1501</ModifiedUserId><CreatedDate>2014-10-13T03:13:21.823-04:00</CreatedDate><CreatedUserId>1501</CreatedUserId></Manufacture><Manufacture diffgr:id="Manufacture4" msdata:rowOrder="3"><ManufacturerId>1264</ManufacturerId><ManufactureName>XYZ</ManufactureName><SalesOfficeId>441</SalesOfficeId><Address /><City /><State /><Zip /><Active>true</Active><ModifiedDate>2014-10-13T03:13:21.83-04:00</ModifiedDate><ModifiedUserId>1501</ModifiedUserId><CreatedDate>2014-10-13T03:13:21.83-04:00</CreatedDate><CreatedUserId>1501</CreatedUserId></Manufacture><Manufacture diffgr:id="Manufacture5" msdata:rowOrder="4"><ManufacturerId>1331</ManufacturerId><ManufactureName>Generic</ManufactureName><SalesOfficeId>441</SalesOfficeId><Address /><City /><State /><Zip /><Active>true</Active><ModifiedDate>2015-01-31T07:36:26.743-05:00</ModifiedDate><ModifiedUserId>1765</ModifiedUserId><CreatedDate>2015-01-31T07:36:26.743-05:00</CreatedDate><CreatedUserId>1765</CreatedUserId></Manufacture><Type diffgr:id="Type1" msdata:rowOrder="0"><ItemType>Coating</ItemType><typ>Coating</typ></Type><Type diffgr:id="Type2" msdata:rowOrder="1"><ItemType>Copper</ItemType><typ>Copper</typ></Type><Type diffgr:id="Type3" msdata:rowOrder="2"><ItemType>Flat</ItemType><typ>Flat</typ></Type><Type diffgr:id="Type4" msdata:rowOrder="3"><ItemType>Metal</ItemType><typ>Metal</typ></Type><Type diffgr:id="Type5" msdata:rowOrder="4"><ItemType>Shake</ItemType><typ>Shake</typ></Type><Type diffgr:id="Type6" msdata:rowOrder="5"><ItemType>Shingles</ItemType><typ>Shingles</typ></Type><Type diffgr:id="Type7" msdata:rowOrder="6"><ItemType>Slate</ItemType><typ>Slate</typ></Type><Type diffgr:id="Type8" msdata:rowOrder="7"><ItemType>Tile</ItemType><typ>Tile</typ></Type><Type diffgr:id="Type9" msdata:rowOrder="8"><ItemType>Underlayment</ItemType><typ>Underlayment</typ></Type><TypePopUp diffgr:id="TypePopUp1" msdata:rowOrder="0"><ItemType>Coating</ItemType><typ>Coating</typ></TypePopUp><TypePopUp diffgr:id="TypePopUp2" msdata:rowOrder="1"><ItemType>Copper</ItemType><typ>Copper</typ></TypePopUp><TypePopUp diffgr:id="TypePopUp3" msdata:rowOrder="2"><ItemType>Cupola</ItemType><typ>Cupola</typ></TypePopUp><TypePopUp diffgr:id="TypePopUp4" msdata:rowOrder="3"><ItemType>Fasteners</ItemType><typ>Fasteners</typ></TypePopUp><TypePopUp diffgr:id="TypePopUp5" msdata:rowOrder="4"><ItemType>Flashing</ItemType><typ>Flashing</typ></TypePopUp><TypePopUp diffgr:id="TypePopUp6" msdata:rowOrder="5"><ItemType>Flat</ItemType><typ>Flat</typ></TypePopUp><TypePopUp diffgr:id="TypePopUp7" msdata:rowOrder="6"><ItemType>Metal</ItemType><typ>Metal</typ></TypePopUp><TypePopUp diffgr:id="TypePopUp8" msdata:rowOrder="7"><ItemType>Shake</ItemType><typ>Shake</typ></TypePopUp><TypePopUp diffgr:id="TypePopUp9" msdata:rowOrder="8"><ItemType>Sheathing Insulation</ItemType><typ>Sheathing Insulation</typ></TypePopUp><TypePopUp diffgr:id="TypePopUp10" msdata:rowOrder="9"><ItemType>Shingles</ItemType><typ>Shingles</typ></TypePopUp><TypePopUp diffgr:id="TypePopUp11" msdata:rowOrder="10"><ItemType>Slate</ItemType><typ>Slate</typ></TypePopUp><TypePopUp diffgr:id="TypePopUp12" msdata:rowOrder="11"><ItemType>Tile</ItemType><typ>Tile</typ></TypePopUp><TypePopUp diffgr:id="TypePopUp13" msdata:rowOrder="12"><ItemType>Underlayment</ItemType><typ>Underlayment</typ></TypePopUp><TypePopUp diffgr:id="TypePopUp14" msdata:rowOrder="13"><ItemType>Vents</ItemType><typ>Vents</typ></TypePopUp></NewDataSet></diffgr:diffgram></GetManufactureAndTypeResult></GetManufactureAndTypeResponse>

</soap:Body>
</soap:Envelope>

使用下面显示的ksoap请求/响应代码请检查: -

public class NewOrderDAO {

private final String BASE_URL = "https://www.maxcononline.com/iosserviceV3.asmx?op=";

private final String NAMESPACE = "http://tempuri.org/";
private final String METHOD_NAME = "GetManufactureAndType";
private final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
private final String URL = BASE_URL + METHOD_NAME;


public SoapObject invokeManufactureAndTypeWS(String SalesOfficeId, String jobtyp) {
    Log.d(getClass().getName(), "Hi I am at under DAO");
    SoapObject response = null;
     int sId = Integer.parseInt(SalesOfficeId);
     String JobType =""+jobtyp;

            // Create request
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        // Property which holds input parameters
        PropertyInfo SalesId = new PropertyInfo();
        PropertyInfo jobType = new PropertyInfo();

            // Set jobType
        jobType.setName("JobType");
        // Set Value
        jobType.setValue(JobType);
        // Set dataType
        jobType.setType(String.class);
        // Add the property to request object
        request.addProperty(jobType);


        // Set SalesId
        SalesId.setName("SalesOfficeId");
        // Set Value
        SalesId.setValue(sId);
        // Set dataType
        SalesId.setType(Integer.class);
        // Add the property to request object
        request.addProperty(SalesId);


        System.out.println("Request is: "+ request);

        // Create envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        // Set output SOAP object
        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;
        envelope.bodyOut = request;
        envelope.encodingStyle = SoapEnvelope.XSD;

        // Create HTTP call object
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;

        try {
            // Invoke web service
         System.out.println("================== "+SOAP_ACTION);
            androidHttpTransport.call(SOAP_ACTION, envelope);

            //Getting response here  
            try{
                response = (SoapObject) envelope.getResponse();
                Log.d("SOAP Response is soapObject:", "== "+ response);
                parseManufactureAndType(response);
            }catch(Exception exception){
                exception.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //Return boolean to calling object
        return null;
    }



private void parseManufactureAndType(SoapObject response) {

    if (response instanceof SoapObject) {
        SoapObject root = (SoapObject) response.getProperty(1);
        Log.i("parseDAta=", "root=" + root.toString());
        // SoapObject s_deals = (SoapObject) root.getProperty("diffgram");
        if (root instanceof SoapObject) {
            if (root.hasProperty("NewDataSet")) {
                SoapObject s_deals_1 = (SoapObject) root.getProperty("NewDataSet");

                Log.v(getClass().getSimpleName(), "s_deals_1 response--------- "+s_deals_1);

                String s_deals_2_2 = (String) s_deals_1.getPropertyAsString("Manufacture");
                Log.v(getClass().getSimpleName(), "Manufacture=-------=  "+s_deals_2_2);

                SoapObject s_deals_2 = (SoapObject) s_deals_1.getProperty("Manufacture");
                Log.v(getClass().getSimpleName(), "s_deals_2 response----------=  "+s_deals_2);
                System.out.println("********s_deals_2 : " + s_deals_2.getPropertyCount());

                SoapObject s_deals_3 = (SoapObject) s_deals_1.getProperty("Type");
                System.out.println("********s_deals_3 : " + s_deals_3.getPropertyCount());

                SoapObject s_deals_4 = (SoapObject) s_deals_1.getProperty("TypePopUp");
                System.out.println("********s_deals_4 : " + s_deals_4.getPropertyCount());



                System.out.println("********Count : " + s_deals_1.getPropertyCount());
                if (s_deals_1.getPropertyCount() > 0) {

                    for (int i = 0; i < s_deals_1.getPropertyCount(); i++) {

                        Log.v(getClass().getName(), "= s_deals_1.getProperty(i)"+ s_deals_1.getProperty(i));

                        Object property = s_deals_1.getProperty(i);
                        if (property instanceof SoapObject) {

                            SoapObject _list = (SoapObject) property;
//Log.i(getClass().getSimpleName(), "getting response===========---------- "+_list);
                            String ManufacturerId = "", ManufactureName = "", SalesOfficeId = "", Address ="", City="", State="", Zip="", Active="", ModifiedDate="", ModifiedUserId="", CreatedDate="", CreatedUserId="";
                            String ItemType ="", typ="";

                            if (_list.hasProperty("ManufacturerId")) {
                                ManufacturerId = _list.getProperty("ManufacturerId").toString();
                                System.out.println("ManufacturerId== " + ManufacturerId);

                            }
                            if (_list.hasProperty("ManufactureName")) {
                                ManufactureName = _list.getProperty("ManufactureName").toString();
                                System.out.println("ManufactureName== " + ManufactureName);

                            }
                            if (_list.hasProperty("SalesOfficeId")) {
                                SalesOfficeId = _list.getProperty("SalesOfficeId").toString();
                                System.out.println("SalesOfficeId== "+ SalesOfficeId);

                            }
                            if (_list.hasProperty("Address")) {
                                Address = _list.getProperty("Address").toString();
                                System.out.println("Address== " + Address);

                            }
                            if (_list.hasProperty("City")) {
                                City = _list.getProperty("City").toString();
                                System.out.println("City== " + City);

                            }
                            if (_list.hasProperty("State")) {
                                State = _list.getProperty("State").toString();
                                System.out.println("State== "+ State);

                            }
                            if (_list.hasProperty("Zip")) {
                                Zip = _list.getProperty("Zip").toString();
                                System.out.println("Zip== " + Zip);

                            }
                            if (_list.hasProperty("Active")) {
                                Active = _list.getProperty("Active").toString();
                                System.out.println("Active== " + Active);

                            }
                            if (_list.hasProperty("ModifiedDate")) {
                                ModifiedDate = _list.getProperty("ModifiedDate").toString();
                                System.out.println("ModifiedDate== "+ ModifiedDate);

                            }
                            if (_list.hasProperty("ModifiedUserId")) {
                                ModifiedUserId = _list.getProperty("ModifiedUserId").toString();
                                System.out.println("ModifiedUserId== " + ModifiedUserId);

                            }
                            if (_list.hasProperty("CreatedDate")) {
                                CreatedDate = _list.getProperty("CreatedDate").toString();
                                System.out.println("CreatedDate== " + CreatedDate);
                            }
                            if (_list.hasProperty("CreatedUserId")) {
                                CreatedUserId = _list.getProperty("CreatedUserId").toString();
                                System.out.println("CreatedUserId== "+ CreatedUserId);

                            }
                            if (_list.hasProperty("ItemType")) {
                                ItemType = _list.getProperty("ItemType").toString();
                                System.out.println("ItemType== "+ ItemType);

                            }
                            if (_list.hasProperty("typ")) {
                                typ = _list.getProperty("typ").toString();
                                System.out.println("typ== "+ typ);

                            }

                        }

                    }
                }

            }
        }
    }


}


}

使用salesOfficeId = 441和jobType =&#34; Roofing&#34;。

运行上面的代码后,我想解析     Manufacture,Type,TypePopUp(单独使用)不使用getProperty(i)解析。

请就上述回复给我适当的解决方案。

1 个答案:

答案 0 :(得分:0)

根据您的代码和我从服务器获取的请求数据附在屏幕截图中......

因此,在Data ItemType中,typ对于整个数据是相同的...... 这就是你得到它的原因..

Data getting From Server

所以,基于此你需要为每个对象制造3个不同的ArrayList,Type和TypePopUp,并根据你选择属性的逻辑,你可以在适当的列表中添加该元素......

喜欢......

  if (_list.hasProperty("ItemType")) 
  {
          ItemType = _list.getProperty("ItemType").toString();
          System.out.println("ItemType== " + ItemType);
          // TODO : add this object to ItemTypeArrayList  
  }

 if (_list.hasProperty("typ")) {
          typ = _list.getProperty("typ").toString();
          System.out.println("typ== " + typ);    
         // TODO : add this object to TypArrayList  
  }