簡體   English   中英

如何訪問xml2js生成的對象中的集合

[英]How can I access collections in an object generated by xml2js

給出以下簡單的XML:

<root>
<node1>
    <node2>
        <node3 name="blah" />
        <node3 name="blah" />
        <node3 name="blah" />
    </node2>
    <node2>
        <node3 name="blah" />
        <node3 name="blah" />
        <node3 name="blah" />
    </node2>
    <node2>
        <node3 name="blah" />
        <node3 name="blah" />
        <node3 name="blah" />
    </node2>
</node1>

和以下javascript:

xml2js.parseString(data, function(err, result) {
            if (err) {
                console.log(err);
            } else {

                console.log(result);

                console.log(result.root);
                console.log(result.root.node1);
                // console.log(result.root.node1.node2);

                // console.log(result.root.node1.node2[1]);

//                console.log(result.root.node1[2]);

  //              console.log(result.root.node1[2]);


            }

一個如何獲得名為node2的子節點(以及該節點的node3)的集合? 在上面我認為似乎沒有任何常識的工作中,沒有任何注釋過的嘗試,而且令人驚訝的是,似乎沒有單個xml2js示例實際上涵蓋了如何訪問其生成的輸出。

JSON.stringify(result)的輸出

{
"root": {
    "node1": [
        {
            "node2": [
                {
                    "node3": [
                        {
                            "$": {
                                "name": "blah"
                            }
                        },
                        {
                            "$": {
                                "name": "blah"
                            }
                        },
                        {
                            "$": {
                                "name": "blah"
                            }
                        }
                    ]
                },
                {
                    "node3": [
                        {
                            "$": {
                                "name": "blah"
                            }
                        },
                        {
                            "$": {
                                "name": "blah"
                            }
                        },
                        {
                            "$": {
                                "name": "blah"
                            }
                        }
                    ]
                },
                {
                    "node3": [
                        {
                            "$": {
                                "name": "blah"
                            }
                        },
                        {
                            "$": {
                                "name": "blah"
                            }
                        },
                        {
                            "$": {
                                "name": "blah"
                            }
                        }
                    ]
                }
            ]
        }
    ]
}

}

一個如何獲得名為node2的子節點的集合?

result.root.node1是一個數組,要在其中獲取第一個<node1> ,必須訪問其索引:

result.root.node1[0].node2

和node3的事情?

沒有一個單一的集合<node3> S,但可以與上述同樣地訪問它們作為.node3相應的第i個的.node2

result.root.node1[0].node2[i].node3

您可以在<node2>集合上使用循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM