关于 xml dom

发布于 2024-11-09 17:16:30 字数 1681 浏览 2 评论 0原文

我有以下 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. Ray</author> 
  <year>2003</year> 
  <price>39.95</price> 
  </book>
  </bookstore>

,现在我正在尝试通过以下代码通过 javascript 获取元素

<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>

,但这仅读取第一个元素,即“Everyday Italian”,并且还显示错误,

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

请提供帮助。

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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

z祗昰~ 2024-11-16 17:16:30

由于每个标题只有 1 个节点,所以应该是

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文