0

我有以下方式的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 将所有“播放器”属性值推送到关联数组(名称/值对)中。

4

2 回答 2

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

演示: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);

演示:http: //jsfiddle.net/2GPBY/3/

于 2013-05-17T05:56:05.983 回答
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)

演示:小提琴

于 2013-05-17T05:56:01.547 回答