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

我正在使用这种 XML 格式,如何使用 Titanium 从中获取国家/地区的名称?

4

3 回答 3

3

至少尝试解析 XML或至少在寻求帮助之前让您知道 DOCS 的位置是一种善意的姿态。但幸运的是,这对于 Titanium 来说非常简单,这里有一个例子:

// 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);
}

粗略检查我提供的链接会向您展示如何执行此操作的 API。

于 2012-08-14T15:23:45.857 回答
1

上面的 Xml 响应是动态的,我自己解析了下面的 Xml 是解决方案

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);
于 2012-08-16T13:31:17.723 回答
0
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();
于 2015-09-21T08:52:33.443 回答