Loading XML file into JavaScript array

I know that this was asked about a million times, however, all the scripts that I found are either spilled out for the person asking the question, or simply do not work at all! I basically limited myself to using regular JavaScript instead of the JQuery library as its for uni assignment, however, I would like to keep it for future use, since sometimes JQuery is not always an option.

In any case, cut to the chase. I have an XML file called "profiles.xml" that contains a fairly simple structure:

 <?xml version="1.0" encoding="iso-8859-1"?>
 <profiles>
 <profile id="1">
     <pic>images/profiles/person1/x.jpg</pic>
     <name>Joe Bloggs</name>
     <nickname>J-Bloggs</nickname>
     <age>21</age>
     <email>j.blogs@me.com</email>
     <role>Web Site Manager</role>
     <likes>
           <like1>Food</like1>
           <like2>Drink</like2>
           <like3>Computing</like3>
           <like4>Music</like4>
     </likes>
     <dislikes>
           <dislike1>Rude People</dislike1>
           <dislike2>Rude People</dislike2>
     </dislikes>
     <favwebsite>http://www.facebook.com</favwebsite>
 </profile>
</profiles>

, , , id. ? , ? , , XPATH .. JavaScript, .

, , , , , , , , script XML HTML-.

, , 4-5 , , , , !

! : -)

+5
1

, JSFiddle/code . 2- .

var profiles = xml.getElementsByTagName("profile");
var arr = [];
for (var key in profiles){
    arr.push([]);
    var nodes = profiles[key].childNodes;
    for (var ele in nodes){  
        if(nodes[ele]){
          arr[key].push(nodes[ele]);
        }
    }
}
console.log(arr);
+9

All Articles