xp

导航

xml文档节点属性的访问

Posted on 2008-04-10 15:50  xp  阅读(386)  评论(0编辑  收藏  举报
<Product ProductSNO="35" ProductName="速度与激情2" />
<Product ProductSNO="36" ProductName="大地雄心" />
<Product ProductSNO="37" ProductName="木乃伊归来" />
<Product ProductSNO="38" ProductName="落叶归根" />
<Product ProductSNO="39" ProductName="太阳花" />
<Product ProductSNO="40" ProductName="憨豆先生的大灾难" />
<Product ProductSNO="41" ProductName="诺丁山" />
<Product ProductSNO="42" ProductName="末世纪暴潮" />
<Product ProductSNO="43" ProductName="星际传奇" />
<Product ProductSNO="44" ProductName="小猪宝贝" />

其中的ProductSNO和ProductName称为节点的属性,DOM解析的方法为
         var root = o.responseXML.documentElement;
                  
         var s = "<br>";
         for(var i=0;i<root.getElementsByTagName(Product ).length;i++)
         {
             s = s + root.getElementsByTagName(Product )[i].attributes[0].text ;            
         }

<Product>
  <ProductSNO>35</ProductSNO>
  <ProductName>速度与激情2</ProductName>
</Product>
<Product>
  <ProductSNO>36</ProductSNO>
  <ProductName>大地雄心</ProductName>
</Product>
<Product>
  <ProductSNO>37</ProductSNO>
  <ProductName>木乃伊归来</ProductName>
</Product>
<Product>
  <ProductSNO>38</ProductSNO>
  <ProductName>落叶归根</ProductName>
</Product>
<Product>
  <ProductSNO>39</ProductSNO>
  <ProductName>太阳花</ProductName>
</Product>
<Product>
  <ProductSNO>40</ProductSNO>
  <ProductName>憨豆先生的大灾难</ProductName>
</Product>
<Product>
  <ProductSNO>41</ProductSNO>
  <ProductName>诺丁山</ProductName>
</Product>
<Product>
  <ProductSNO>42</ProductSNO>
  <ProductName>末世纪暴潮</ProductName>
</Product>
<Product>
  <ProductSNO>43</ProductSNO>
  <ProductName>星际传奇</ProductName>
</Product>
<Product>
  <ProductSNO>44</ProductSNO>
  <ProductName>小猪宝贝</ProductName>
</Product>

其中ProductSNO和ProductName成为子节点,DOM解析的方法为
var root = o.responseXML.documentElement;
                  
         var s = "<br>";
         for(var i=0;i<root.getElementsByTagName(Product ).length;i++)
         {
             s = s + root.getElementsByTagName(Product )[i].childNodes[0].nodeValue;            
         }