How to parse this XML in Titanium>

2019-08-22 09:34发布

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

3条回答
Viruses.
2楼-- · 2019-08-22 09:45

It would be a kind gesture to at least attempt to parse the XML, or at least show you know where the DOCS are before asking for help. But thankfully this is very straightforward with Titanium, here is an example:

// Load file containing XML from resources directory
var xmlFile = Ti.Filesystem.getFile('countries.xml');

// Get contents(as blob) from file
var contents = xmlFile.read();

// Turn contents into a XMLDomDocument for parsing (get string from blob first)
var xmlDomDoc = Ti.XML.parseString(contents.text);

// Get all the item tags and their contents
var allItemTags = xmlDomDoc.getElementsByTagName('item');

// Loop over them and grab the text contents and print
for (var i = 0; i < allItemTags.length; i++) {
    var countryName = allItemTags.item(i).textContent;
    Ti.API.info(countryName);
}

A cursory examination of the link I provided would have showed you the API on how do this.

查看更多
Anthone
3楼-- · 2019-08-22 09:50

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);
查看更多
ゆ 、 Hurt°
4楼-- · 2019-08-22 09:53
var win = Titanium.UI.createWindow({  
    title:"XHR",
    backgroundColor:"#FFFFFF",
    exitOnClose:true
});

var data = [];//We'll fill this table after the xml has loaded
var tableView = Titanium.UI.createTableView({
    data:data
});

function errorMessage(){
    alert("Well, that didn't work");
}

function renderXML(){
    var tours = this.responseXML.documentElement;
    var tour = tours.getElementsByTagName("tour");

    //traverse the tour node, pull out the titles
    for(var i=0; i<tour.length;i++){
        var item = tour.item(i);
        var title = item.getElementsByTagName("tourTitle").item(0).text;

        var row = Titanium.UI.createTableViewRow({
            title:title,
            className:"tableRow",
        });
        data.push(row);
    }

    tableView.setData(data);

}

var xhr = Titanium.Network.createHTTPClient({
    onload: renderXML,
    onerror: errorMessage
});

win.add(tableView);

xhr.open("GET","http://services.explorecalifornia.org/rest/tours.php");
xhr.send();

win.open();
查看更多
登录 后发表回答