将XML文件加载到JavaScript数组中

时间:2021-06-17 21:21:28

I know this has been asked about a million times over, however all the scripts I've found either are taylored for the person asking the question or just don't work at all! Basically I'm limited to using normal JavaScript instead of a library like JQuery as its for a uni assignment, however would like to have stored for future use as sometimes JQuery isn't always an option.

我知道这已经被问了大约一百万次了,但是我发现的所有脚本都是针对提问的人或者根本不工作的人!基本上我只限于使用普通的JavaScript而不是像JQuery这样的库作为单一赋值,但是希望存储以备将来使用,因为有时JQuery并不总是一个选项。

Anyway cutting to the chase. I have an XML file called "profiles.xml" which contains a fairly simple structure:

无论如何切入追逐。我有一个名为“profiles.xml”的XML文件,它包含一个相当简单的结构:

 <?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>

So basically I'm wanting to load each profile into a seperate new array each time it needs to be accessed but in addition to that would like to do it based on the "id" attribute value. Is this possible? If so how? I've not got any time left to be learning stuff like XPATH etc... This needs to be done using JavaScript and that alone no server side scripting is allowed.

所以基本上我想要在每次需要访问时将每个配置文件加载到一个单独的新数组中,但除此之外,它还希望基于“id”属性值来完成。这可能吗?如果是这样的话?我没有时间去学习XPATH之类的东西......这需要使用JavaScript来完成,而且只允许使用服务器端脚本。

Also as mentioned some scripts just simply don't work as when I try to put it into functions it comes up saying that certain variables aren't defined even when they have been set so need the script to be passing all the information from the XML file into the new Array and have it readily accessible throughout the entire HTML document.

同样如前所述,一些脚本只是不起作用,因为当我尝试将它放入函数时,它会说某些变量即使已经设置也没有定义,所以需要脚本从XML传递所有信息将文件放入新数组中,并在整个HTML文档中随时访问它。

Please someone help, I've spent the past 4-5 weeks trying to work this out on my own as well as many sleepless nights searching on the net for scripts that might give me a clue!

请有人帮忙,过去4-5周我一直在尝试自己解决这个问题以及许多不眠之夜在网上搜索可能给我一些线索的脚本!

Any help is muchly appreciated! Thanks :-)

任何帮助非常感谢!谢谢 :-)

1 个解决方案

#1


10  

Unless I don't fully understand your issue the JSFiddle/code below should help. I used a 2-d array.

除非我不完全理解您的问题,否则下面的JSFiddle /代码应该有所帮助。我使用了二维数组。

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);

#1


10  

Unless I don't fully understand your issue the JSFiddle/code below should help. I used a 2-d array.

除非我不完全理解您的问题,否则下面的JSFiddle /代码应该有所帮助。我使用了二维数组。

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);