将 XML 文件加载到 JavaScript 数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10675216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading XML file into JavaScript Array
提问by GeordieDave1980
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 这样的库作为它的 uni 分配,但是我想存储以备将来使用,因为有时 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>[email protected]</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 :-)
非常感谢任何帮助!谢谢 :-)
回答by marteljn
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);