Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...