加载XML文件转换为JavaScript阵列 [英] Loading XML file into JavaScript Array

查看:140
本文介绍了加载XML文件转换为JavaScript阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已被要求一百万遍,但所有的脚本,我发现无论是taylored的人问的问题或只是不工作!基本上我有限的使用,而不是像jQuery库正常的JavaScript作为其UNI端口分配,但希望有保存以备将来使用,因为有时候jQuery是并不总是一个选择。

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.

反正切正题。我有一个名为的profiles.xml的XML文件,其中包含一个相当简单的结构:

Anyway cutting to the chase. I have an XML file called "profiles.xml" which 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和单独没有服务器端脚本允许完成。

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.

也提到一些脚本只是简单的不工作,当我试图把它变成它的功能上来说,即使他们已经确定因此需要脚本被路过的所有信息某些变量没有定义从XML文件到新阵列,并有在整个HTML文档它易于接近。

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.

请别人帮忙,我已经在过去的4-5周努力工作,这一点对我自己以及许多不眠之夜在网上搜索脚本可能给我一个线索!

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!

任何帮助,长久AP preciated!感谢: - )

Any help is muchly appreciated! Thanks :-)

推荐答案

除非我不完全理解你的问题,下面的jsfiddle / code应该有所帮助。我用了一个2-D数组。

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

http://jsfiddle.net/KLPFR/

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

这篇关于加载XML文件转换为JavaScript阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆