<?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获取这些国家/地区的名称?
答案 0 :(得分:3)
在寻求帮助之前,至少尝试解析XML or at least show you know where the DOCS are是一种善意的姿态。但幸运的是,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);
}
粗略检查我提供的链接会向您展示如何做到这一点。
答案 1 :(得分: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);
答案 2 :(得分: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();