Search code examples
titaniumtitanium-mobile

How to parse this XML in Titanium>


<?xml version="1.0" ?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SOAP-ENV:Body>
    <ns1:getCountriesResponse xmlns:ns1="SoapQrcode">
      <return SOAP-ENC:arrayType=":[8]" xsi:type="SOAP-ENC:Array">
        <item xsi:type="xsd:string">
          China
        </item>
        <item xsi:type="xsd:string">
          France
        </item>
     </return>
    </ns1:getCountriesResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I am using this XML format, how can I get the names of the countries from this with Titanium?


Solution

  • The above Xml response was dynamic , I have myself parsed the Xml below is the solution

    Ti.API.info('Original response ' + this.responseText);
    
    // Document Object
    var doc = this.responseXML.documentElement;
    
    Ti.API.info('Document XML' + doc);
    
    var elements = doc.getElementsByTagName("item");
    
    Ti.API.info('Document item' + elements.text);
    
    //this is the XML document object
    
    // Parse XML
    var xml = Ti.XML.parseString(this.responseText);
    Ti.API.info('Parsed XML' + xml);
    
    // get item from xml
    var countryList = xml.documentElement.getElementsByTagName("item");
    
    Titanium.API.info("country is " + countryList);
    Titanium.API.info("lenght of countries" + countryList.length);
    Titanium.API.info("country is " + countryList.item(0));
    
    for (var i = 0; i < countryList.length; i++) {
        Titanium.API.info("loop country is " + countryList.item(i).text);
        countryArray[i] = countryList.item(i).text;
    };
    
    Titanium.API.info("Array country is " + countryArray);