在以下代码摘录中,products 是一个 xml 节点列表,其结构为:
<products>
<product>
<id>1</id>
<name>item1</name>
</product>
<product>
<id>2</id>
<name>item2</name>
</product>
</products>
xml = httpRequest.responseXML
products = xml.getElementsByTagName('products')
products[0].constructor
//=>ElementConstructor
product = products[0]
//=><product>…</product>
product.constructor
//=>ElementConstructor
product.getElementsByTagName('id')
//[ <id>1</id> ]
for(product in products){product.getElementsByTagName('id')}
//=>TypeError: 'undefined' is not a function (evaluating 'product.getElementsByTagName('id')')
如何迭代<product>
元素中的每个元素<products>
,并仅选择标签