请问这个XML文件中的内容如何用JS语法解析出来?

progray 2005-04-13 10:27:35

<?xml version="1.0" encoding="ISO-8859-1"?>
<nitf>
<body>
<body.head>
<byline>
<aa name="aa" x="0" y="0" />
<bb name="aa" x="0" y="0" />
<cc name="aa" x="0" y="0" />
<dd name="aa" x="0" y="0" />
</byline>
</body.head>
</body>
</nitf>
其中nitf、body这些节点我都能用getElementsByTagName("nitf")类似的语句找到,并且可以通过
objlist.length来遍历它的每个子节点,但aa 、bb这些没有对应的/aa, /bb这样的节点,怎么才能遍历得到呢?并且可以取得它的name,x,y的值。
我接触xml不久,恳请大家帮忙。
...全文
87 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
progray 2005-04-13
  • 打赏
  • 举报
回复
谢谢eglic(圪圪),我刚知道如果aa、bb下面没有子节点,用getElementsByTagName("byline")取他们的父节点的length是0,那我到底该怎么得到这些节点和呢?

实在是晕头了,还请eglic(圪圪)不吝赐教。
是是非非 2005-04-13
  • 打赏
  • 举报
回复
<aa name="aa" x="0" y="0" />

<aa name="aa" x="0" y="0"></aa>
是一样的
progray 2005-04-13
  • 打赏
  • 举报
回复
好,多谢,十分感谢!
其实我本该查资料的,但现在还在做其他的,实在是心烦意乱了,多谢你的帮助!
scoutlin 2005-04-13
  • 打赏
  • 举报
回复
<html>
<script>
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.load("1.xml")
node=xmlDoc.selectSingleNode("//byline")
for(var j=0;j<node.childNodes.length;j++)
{
document.write(node.childNodes[j].text);
document.write(node.childNodes[j].getAttribute("x"));
}
</script>
</html>
progray 2005-04-13
  • 打赏
  • 举报
回复
scoutlin(8da子民)(www.8da.cc)
实在不好意思,还要麻烦你.我这样写了,但提示"'node[...].children.length'为空域或不是对象"
我把xml和html文件都列出来。麻烦你看一下,我是winxp sp2不知道何系统环境有没有关系,每次执行脚本前都要弹出一个信息栏,我要选“允许阻止的内容”才可以执行。

xml文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<nitf>
<body>
<body.head>
<byline>
<aa name="aa" x="0" y="0"></aa>
<bb name="bb" x="1" y="4"></bb>
<cc name="cc" x="2" y="5"></cc>
<dd name="dd" x="3" y="6"></dd>
</byline>
</body.head>
</body>
</nitf>

html文件:

<html>
<script>
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.load("test.xml")
node=xmlDoc.selectNodes("//byline")
for(var i=0;i<node.length;i++)
for(var j=0;j<node[i].children.length;j++)
{
document.write(node[i].children[j].text);
document.write(node[i].children[j].getAttribute("x"));
}
</script>
</html>

scoutlin 2005-04-13
  • 打赏
  • 举报
回复
node=xmlDoc.selectNodes("//byline")
for(var i=0;i<node.length;i++)
for(var j=0;j<node[i].children.length;j++)
{
document.write(node[i].children[j].text);
document.write(node[i].children[j].getAttribute("x"));

}
progray 2005-04-13
  • 打赏
  • 举报
回复
谢谢scoutlin(8da子民)(www.8da.cc)
你给出的是如何获得aa的属性值,但我的问题还有一个,就是 <byline>所包含的那些aa,bb,这些是不确定的,动态生成的,该怎么获得这些节点呢?
scoutlin 2005-04-13
  • 打赏
  • 举报
回复
node=xmlDoc.selectSingleNode("//aa")
a=node.getAttribute("aa")
x=node.getAttribute("x")

8,909

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧