【问题标题】:How to parse this XML in Titanium>如何在 Titanium 中解析这个 XML>
【发布时间】:2012-08-14 05:18:29
【问题描述】:
<?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 从中获取国家/地区的名称?

【问题讨论】:

    标签: titanium titanium-mobile


    【解决方案1】:

    在寻求帮助之前,至少尝试解析 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);
    }
    

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

    【讨论】:

      【解决方案2】:

      上面的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);
      

      【讨论】:

        【解决方案3】:
        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();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多