0

I have this XML document

<xml>
 <word>
   <threeletter>rip</threeletter>
   <fourletter>Pier</fourletter> 
   <fiveletter>Spire</fiveletter>
   <sixletter>Spider</sixletter>
 </word>
 <word>
   <threeletter>rip</threeletter>
   <fourletter>Pier</fourletter> 
   <fiveletter>Spire</fiveletter>
   <sixletter>Spider</sixletter>
 </word>
 <word>
   <threeletter>rip</threeletter>
   <fourletter>Pier</fourletter> 
   <fiveletter>Spire</fiveletter>
   <sixletter>Spider</sixletter>
 </word>
</xml>

Let's assume that each element inside the word has different values. is it possible in javascript to retrieve all of these values? then store them in an array? or a variable? if so how? because I am planning to compare them with user inputs

user962206
  • 15,637
  • 61
  • 177
  • 270
  • 1
    possible duplicate of http://stackoverflow.com/questions/649614/xml-parsing-in-javascript – T I Mar 28 '12 at 15:57

2 Answers2

2

It's not at all clear how you want your array structured.

Here is one example that store multiple arrays in an object

var words={ threeletter:[], fourletter:[], fiveletter:[], sixletter:[]};
var xmlItems=['threeletter','fourletter', 'fiveletter', 'sixletter'];

$(xml).find('word').each(function(){
    var $word=$(this);
    $.each( xmlItems, function(i, item){
        words[item].push( $word.find( item ).text());
    });    
});

To access 3 letter array use words.threeletter

Demo: http://jsfiddle.net/5EnR2/

charlietfl
  • 170,828
  • 13
  • 121
  • 150
1

Two ways :):

Luca
  • 4,223
  • 1
  • 21
  • 24