我有以下方式的XML文件:
<root>
<player>
<roster>
<player code="AUQ" position="Guard" countrycode="SRB"/>
<player code="AQX" position="Forward" countrycode="GER"/>
</roster>
</player>
<player>
<roster>
<player code="AUQ1" position="Guard" countrycode="SRB"/>
<player code="AQX1" position="Forward" countrycode="GER"/>
</roster>
</player>
</root>
如何使用javascript将所有'player'属性值推送到关联数组(名称/值对)。
答案 0 :(得分:1)
由于每个播放器(code
,position
,countrycode
)的密钥相同,因此您无法使用关联数组(在JavaScript中,实际上没有一个,但您可以使用Object
)。你必须使用一组对象。我就是这样做的:
var str = '<root><player><roster><player code="AUQ" position="Guard" countrycode="SRB"/><player code="AQX" position="Forward" countrycode="GER"/></roster></player><player><roster><player code="AUQ1" position="Guard" countrycode="SRB"/><player code="AQX1" position="Forward" countrycode="GER"/></roster></player></root>',
oParser = new DOMParser(),
xmlDoc = oParser.parseFromString(str, "text/xml");
var players = xmlDoc.getElementsByTagName("player"),
ret = [], i, j, cur;
for (i = 0, j = players.length; i < j; i++) {
cur = players[i];
if (cur.hasAttribute("code")) {
ret.push({
code: cur.getAttribute("code"),
position: cur.getAttribute("position"),
countrycode: cur.getAttribute("countrycode")
});
}
}
console.log(ret);
DEMO: http://jsfiddle.net/2GPBY/1/
如果要为该键的所有值创建一个键映射,可以使用:
var str = '<root><player><roster><player code="AUQ" position="Guard" countrycode="SRB"/><player code="AQX" position="Forward" countrycode="GER"/></roster></player><player><roster><player code="AUQ1" position="Guard" countrycode="SRB"/><player code="AQX1" position="Forward" countrycode="GER"/></roster></player></root>',
oParser = new DOMParser(),
xmlDoc = oParser.parseFromString(str, "text/xml");
var players = xmlDoc.getElementsByTagName("player"),
ret = {}, i, j, cur;
for (i = 0, j = players.length; i < j; i++) {
cur = players[i];
if (cur.hasAttribute("code")) {
if (!("code" in ret)) {
ret.code = [];
}
ret.code.push(cur.getAttribute("code"));
if (!("position" in ret)) {
ret.position = [];
}
ret.position.push(cur.getAttribute("position"));
if (!("countrycode" in ret)) {
ret.countrycode = [];
}
ret.countrycode.push(cur.getAttribute("countrycode"));
}
}
console.log(ret);
答案 1 :(得分:0)
尝试
function loadXml(xml){
if (window.DOMParser) {
parser=new DOMParser();
xmlDoc=parser.parseFromString(xml, "text/xml");
} else {
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(xml);
}
return xmlDoc;
}
var xml = '<root> <player> <roster> <player code="AUQ" position="Guard" countrycode="SRB"/> <player code="AQX" position="Forward" countrycode="GER"/></roster></player><player><roster><player code="AUQ1" position="Guard" countrycode="SRB"/><player code="AQX1" position="Forward" countrycode="GER"/></roster></player></root>'
var doc = loadXml(xml);
var rosters = doc.getElementsByTagName('roster');
var array = [];
for(var i = 0; i < rosters.length; i++){
var roser = rosters[i];
var players = roser.getElementsByTagName('player');
for(var j = 0; j < players.length; j++){
var player = players[j], attributes = player.attributes;
var obj = {};
for (var key in attributes) {
if (attributes.hasOwnProperty(key) && attributes[key].nodeName) {
obj[attributes[key].nodeName] = attributes[key].value;
}
}
array.push(obj)
}
}
console.log(array)
演示:Fiddle