开发者

regarding xml dom

i have the following xml

<?xml version="1.0" encoding="ISO-8859-1" ?> 
 <bookstore>
 <book category="cooking">
  <title lang="en">Everyday Italian</title> 
  <author>Giada De Laurentiis</author> 
  <year>2005</year> 
  <price>30.00</price> 
  </book>
 <book category="children">
  <title lang="en">Harry Potter</title> 
  <author>J K. Rowling</author> 
  <year>2005</year> 
  <price>29.99</price> 
  </book>
 <book category="web">
  <title lang="en">XQuery Kick Start</title> 
  <author>James McGovern</author> 
  <author>Per Bothner</author> 
  <author>Kurt Cagle</author> 
  <author>James Linn</author> 
  <author>Vaidyanathan Nagarajan</author> 
  <year>2003</year> 
  <price>49.99</price> 
  </book>
 <book category="web" cover="paperback">
  <title lang="en">Learning XML</title> 
  <author>Erik T开发者_StackOverflow社区. Ray</author> 
  <year>2003</year> 
  <price>39.95</price> 
  </book>
  </bookstore>

and now i am trying o get the elements through javascript by following code

<script type="text/javascript">
var doc=new ActiveXObject("Microsoft.XMLDOM");
doc.async="true";
doc.load("test.xml");
var x=doc.getElementsByTagName("title");
for(var i=0;i<x.length;i++)
{
document.write(x[i].childNodes[i].nodeValue);
}
</script>

but this reads only the first element i.e. "Everyday Italian" and also shows an error

Message: Object required
Line: 11
Char: 1
Code: 0

any help out there please.


Since each title has only 1 node, so it should be

<script type="text/javascript">
  var doc=new ActiveXObject("Microsoft.XMLDOM");
  doc.async="true";
  doc.load("test.xml");
  var x=doc.getElementsByTagName("title");
  for(var i=0;i<x.length;i++)
  {
    document.write(x[i].childNodes[0].nodeValue); // replace i with 0 in childNodes
  }
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜