Question

I am using xui js in PhoneGap and I want to read xml through xui js.I have written some code and I am getting root Element successfully.And running forloop for each element I am also getting element that is able to display textContent.But I am not able to get further elements inside tags in element.How can I get that.My code is as follows:

    xui(window).on('load', function(){
        var url="http://www.w3schools.com/dom/books.xml";
        fireURL(url,winner);
    });

    function fireURL(url,success)
    {   
        var option={
        async:true,
        callback:function(){
            success(this.responseText);
        }
        }
        x$().xhr(url,option);
    } 

   function winner(data)
    {    
         domParser = new DOMParser();
         var xmlDoc=domParser.parseFromString(data,"text/xml");

        x$(xmlDoc).find("book").each(function(element,index){
            alert(element.textContent);
            //Here I want to get further elements by its tag name
        });
    }
Was it helpful?

Solution

You can easily use javascript for that.Get reference from Here

Or Replace your success code with following code:-

        function winner(data)
    {    
         domParser = new DOMParser();
         var xmlDoc=domParser.parseFromString(data,"text/xml");

         var _bookStore=[];

        x$(xmlDoc).find("book").each(function(element,index){
              var _book={};
              _book.author=[];
              _book.title=element.getElementsByTagName("title")[0].textContent;
              for(j=0;j<element.getElementsByTagName("author").length;j++){
                  _book.author.push(element.getElementsByTagName("author")[j].textContent);
              }
              _book.year=element.getElementsByTagName("year")[0].textContent;
              _book.price=element.getElementsByTagName("price")[0].textContent;
              _bookStore.push(_book);
        });

         alert("hello"+JSON.stringify(_bookStore));
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top