简体   繁体   中英

using javascript to parse XML string

I have XML file in the following format,

In data1 array, i am getting first 'webportal:highResUrl' value, but not all values (ie 'webportal:highResUrl' contains 2 values). I am getting repeated values in data1 array.

<root>
<item>
 <webportal:files>
  <webportal:highResUrl>http://xxx.xom/image</webportal:highResUrl>
  <webportal:highResUrl>http://xxx.xom/image</webportal:highResUrl>     
 </webportal:files> 
</item>
<item>
<webportal:files>
  <webportal:highResUrl>http://xxx.xom/image</webportal:highResUrl>
  <webportal:highResUrl>http://xxx.xom/image</webportal:highResUrl>     
 </webportal:files>
</item>
</root>



 var data = []; 
    var data1=[];   
    var xhr = Ti.Network.createHTTPClient();        
    xhr.onload = function()
    {       
            var doc = this.responseXML.documentElement;
            var items = doc.getElementsByTagName("item");       
            for (var c=0;c<items.length;c++)
            {                   
                var item = items.item(c);
                var sTitle1 = item.getElementsByTagName("title").item(0).text;
                var itemswebportal=item.getElementsByTagName("webportal:files");

                    for(var j=0;j<itemswebportal.length;j++){   

                        var sTeamHighResouImage = item.getElementsByTagName("webportal:highResUrl").item(0).text
                        data1.push({
                        path: sTeamHighResouImage,
                        });             
                     }}}
    };
    xhr.send(); 

Because you are getting only one value from the <webportal:highResUrl>

item.getElementsByTagName("webportal:highResUrl").item(0).text

The above line returns only the text of first <webportal:highResUrl> . It seems to be a static one so just make it over a loop to get all the values

for(var j=0;j<itemswebportal.length;j++){   
   for(var k=0;k<item.getElementsByTagName("webportal:highResUrl").length;k++)
                     {
                        var sTeamHighResouImage = item.getElementsByTagName("webportal:highResUrl").item(k).text
                        data1.push({
                        path: sTeamHighResouImage,
                        });             
                     }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM