iammatthew2
iammatthew2

Reputation: 659

Parse XML into Javascript Object

I am grabbing data from a Google spreadsheet through the Google API using cURL in PHP. Using a AJAX HTTP request (via jQuery) I can pull all the data in and get it into an array, but since the <content> tag looks like dirty JSON I'm a little stuck.

I would like to be able to reference the data as a JS object, like so:

alert(xml.feed.content.name);

Example Code:

$.ajax({
   type: "GET",
   url: GtargetURL,
   dataType: "xml",
   success: function parseMyXML(xml){
      var Entries = new Array;
      var i = 0;
      $(xml).find("entry").each(function(){
         var content = $(this).find("content").text();
         Entries[i]=content;
         i++;
      });
      var myArray= new Array();
      myArray= Entries[1].split(",");
      alert (myArray[1]); // Result: "test2"
   }        
});

Example XML:

<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gsx=
<entry>
<content type='text'>relativeid: 4, name: test2, type:  teset3, multiples: yes, cat: yes</content>
</entry>
<entry>many more entries...</entry>
</feed>

Thanks for any help you can offer.

For what it's worth, I am using this URL format for the Google api call:

https://spreadsheets.google.com/feeds/list/KEY-ID-HERE/1/public/basic

I know that I can do a "cell" call instead of a "list" call, but this suits my purposes better.

Upvotes: 2

Views: 4588

Answers (2)

Tim
Tim

Reputation: 2440

You could do something like this: http://jsfiddle.net/GVUnF/ http://jsfiddle.net/rMMkD/1/ in your loop.

var jsonLikeString = "name:red, type:blue, multiples:green, cat:brown";

var jsObject = {};

var stringWithoutSpaces = jsonLikeString.split(' ').join('');
var splitStrings = stringWithoutSpaces.split(",");
var kvPairArray = [];

for(var i in splitStrings){
    if(splitStrings.hasOwnProperty(i)){     
        var kvPair = splitStrings[i];
        kvPairArray = kvPair.split(":");
        jsObject[kvPairArray[0]] = kvPairArray[1];

    }
}
alert(jsObject.cat);

Please note that

var foo = new Array;

is not exactly idiomatic in javascript. You should use

var foo = [];

instead.

Also, for appending to an array you should use

foo.push('something');

instead of having a variable i and incrementing it every loop.

Upvotes: 3

Ruan Mendes
Ruan Mendes

Reputation: 92274

There are two parts to your question:

  • How to turn XML into JSON
  • How to turn some text in XML that is almost JSON into JSON

To make XML into JSON you can use a library like http://www.thomasfrank.se/xml_to_json.html

It turns

<animals>
     <dog>
          <name>Rufus</name>
          <breed>labrador</breed>
     </dog>
     <dog>
          <name>Marty</name>
          <breed>whippet</breed>
     </dog>
     <cat name="Matilda"/>
</animals>

Into

{"animals":
    {"dog":[
        {"name":"Rufus",
         "breed":"labrador"},
        {"name":"Marty",
         "breed":"whippet"}],
     "cat":
        {"name":"Matilda"}}}

Then you can follow the suggestion from TheShellfishMeme to turn

relativeid: 4, name: test2, type:  teset3, multiples: yes, cat: yes

Into a JSON object

Upvotes: 3

Related Questions