为什么?Document.documentElement.firstChild.nodeValue的值为NUll?

lasting 2001-05-24 07:15:00
Document.documentElement.firstChild.nodeValue的值为NUll,代码如下?
我想得到title下在text,不要子节点的text。
<?xml version="1.0"?>
<!-- File Name: Book.xml -->
<BOOK>
<TITLE>The Adventures of Huckleberry Finn<test>this is a test</test></TITLE>
<AUTHOR>Mark Twain</AUTHOR>
<BINDING>mass market paperback</BINDING>
<PAGES>298</PAGES>
<PRICE>$5.49</PRICE>
</BOOK>


<!--This is :domDeom.htm//-->
<html>
<head>
<title>Book Description</title>
<script language="Javascript" for="window" Event="ONLOAD">
Document = dsoBook.XMLDocument;
comment.innerText = document.childNodes(0).text;
title.innerText = Document.documentElement.firstChild.nodeValue;
author.innerText = Document.documentElement.childNodes(1).text.length;
binding.innerText=Document.documentElement.childNodes(2).text;
pages.innerText = Document.documentElement.childNodes(3).text;
price.innerText = Document.documentElement.childNodes(4).text;
</script>
</head>
<body>
<XML ID="dsoBook" SRC="Book.xml"></XML>
<span id="comment"></span><p>
<h2>Book Description</h2>
<span style="font-style:italic">TITLE:</span>
<span id="title" style="font-weight:bold"></span>
<P>
<span style="font-style:italic">AUTHOR:</span>
<span id="author" style="font-weight:bold"></span>
<P>
<span style="font-style:italic">binding:</span>
<span id="binding" style="font-weight:bold"></span>
<P>
<span style="font-style:italic">pages:</span>
<span id="pages" style="font-weight:bold"></span>
<P>
<span style="font-style:italic">price:</span>
<span id="price" style="font-weight:bold"></span>
</body>
</html>
...全文
219 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xujiaqiang 2001-05-30
  • 打赏
  • 举报
回复
解析器并没有忽略空格和回车换行符,第一个子节点是换行符
"The Adventures of Huckleberry Finn"是title的子节点。
用getElementsByTagName("title").item(0).getFirstChild().getNodeValue();
x86 2001-05-29
  • 打赏
  • 举报
回复
这样就可以了:
alert(Document.documentElement.childNodes(0).childNodes(0).text);
但是为什么要这样呢?
看看下面这个html:
<!-- File Name: ShowNodes.htm -->

<HTML>

<HEAD>

<TITLE>Show DOM Nodes</TITLE>

<SCRIPT LANGUAGE="JavaScript" FOR="window" EVENT="ONLOAD">
/* get Document node: */
Document = dsoXML.XMLDocument;

/* start by passing the Document node to DisplayNodes: */
DisplayDIV.innerText = DisplayNodes(Document, 0);

function DisplayNodes (Node, IndentLevel)
{
/* declare local variables for recursion: */
var i;
var DisplayString = "";

/* build up the indentation for this level: */
Indent = "";
IndentDelta = "..";
for (i=0; i < IndentLevel; ++i)
Indent += IndentDelta;

/* display the current node's properties: */
DisplayString += Indent + "nodeName: "
+ Node.nodeName + "\n"
+ Indent + "nodeTypeType: "
+ Node.nodeType + "\n"
+ Indent + "nodeTypeString: "
+ Node.nodeTypeString + "\n"
+ Indent + "nodeValue: "
+ Node.nodeValue + "\n\n";

/* display each of the node's attribute child nodes: */
Indent += IndentDelta;
for (i=0; Node.attributes != null && i < Node.attributes.length; ++i)
DisplayString += Indent + "nodeName: "
+ Node.attributes(i).nodeName + "\n"
+ Indent + "nodeTypeType: "
+ Node.attributes(i).nodeType + "\n"
+ Indent + "nodeTypeString: "
+ Node.attributes(i).nodeTypeString
+ "\n"
+ Indent + "nodeValue: "
+ Node.attributes(i).nodeValue
+ "\n\n";

/* display each of the node's nonattribute child
nodes: */
for (i=0; i < Node.childNodes.length; ++i)
DisplayString += DisplayNodes (Node.childNodes(i), IndentLevel + 1);

/* return the string containing the results: */
return DisplayString;
}
</SCRIPT>
</HEAD>

<BODY >
<XML ID="dsoXML" SRC="Book.xml"></XML>

<DIV ID="DisplayDIV"></DIV>
</body>
</HTML>

========= 显示结果 =========================
nodeName: #document
nodeTypeType: 9
nodeTypeString: document
nodeValue: null

..nodeName: xml
..nodeTypeType: 7
..nodeTypeString: processinginstruction
..nodeValue: version="1.0"

....nodeName: version
....nodeTypeType: 2
....nodeTypeString: attribute
....nodeValue: 1.0

..nodeName: #comment
..nodeTypeType: 8
..nodeTypeString: comment
..nodeValue: File Name: Book.xml

..nodeName: BOOK
..nodeTypeType: 1
..nodeTypeString: element
..nodeValue: null

....nodeName: TITLE
....nodeTypeType: 1
....nodeTypeString: element
....nodeValue: null

......nodeName: #text
......nodeTypeType: 3
......nodeTypeString: text
......nodeValue: The Adventures of Huckleberry Finn

......nodeName: test1
......nodeTypeType: 1
......nodeTypeString: element
......nodeValue: null

........nodeName: #text
........nodeTypeType: 3
........nodeTypeString: text
........nodeValue: this is a test

....nodeName: AUTHOR
....nodeTypeType: 1
....nodeTypeString: element
....nodeValue: null

......nodeName: #text
......nodeTypeType: 3
......nodeTypeString: text
......nodeValue: Mark Twain

....nodeName: BINDING
....nodeTypeType: 1
....nodeTypeString: element
....nodeValue: null

......nodeName: #text
......nodeTypeType: 3
......nodeTypeString: text
......nodeValue: mass market

......nodeName: ttt
......nodeTypeType: 1
......nodeTypeString: element
......nodeValue: null

........nodeName: #text
........nodeTypeType: 3
........nodeTypeString: text
........nodeValue: pap

......nodeName: #text
......nodeTypeType: 3
......nodeTypeString: text
......nodeValue: erback

....nodeName: PAGES
....nodeTypeType: 1
....nodeTypeString: element
....nodeValue: null

......nodeName: #text
......nodeTypeType: 3
......nodeTypeString: text
......nodeValue: 298

....nodeName: PRICE
....nodeTypeType: 1
....nodeTypeString: element
....nodeValue: null

......nodeName: #text
......nodeTypeType: 3
......nodeTypeString: text
......nodeValue: $5.49

8,906

社区成员

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

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