node.js覆盖params对象

时间:2014-03-27 17:34:18

标签: xml node.js

我从Node.js开始,我遇到以下问题:
我有一个XM fileL来解析不同的节点,我想提取信息并输入Array传递给一个表单:

var nodes = xpath.select("//return_address", doc)

var addresses = [];

for (i=0;i<nodes.length;i++){ 
    var node =nodes[i];
    var address = {
        code:   xpath.select1("//return_address/@code", node),
        house_no:       xpath.select1("//return_address/@house_no", node),
        postcode:      xpath.select1("//return_address/@postcode", node)
              };

    addresses.push(address);
}

//exit:
for (i=0;i<addresses.length;i++){ 
    console.log("addresses.code "+addresses[i].code.value); 
    console.log("         .house_no:    "+addresses[i].house_no.value); 
    console.log("         .postcode:    "+addresses[i].postcode.value); 
}

这是以下结果:addresses.length = 3

addresses.ptc_abs_code 58150004026

     .house_no:    27

     .postcode:    BA133BN

addresses.ptc_abs_code 58150004026

     .house_no:    27

     .postcode:    BA133BN

addresses.ptc_abs_code 58150004026

     .house_no:    27


     .postcode:    BA133BN

我有三个不同的节点,但它总是打印出最后一个节点,为什么会发生这种情况?

1 个答案:

答案 0 :(得分:-1)

你需要在i = 0

之前放置var
for (var i=0;i<nodes.length;i++)

幸运的是Node.js并不像浏览器那样允许。