当前位置: 首页 > 网络学院 > XML相关教程 > XML DOM > DOM 访问节点树

XML DOM
DOM 节点
DOM 节点列表
DOM 解析
DOM 遍历节点树
DOM Mozilla 和 IE
DOM 获取节点
DOM 设置节点
DOM 删除节点
DOM 更换节点
DOM 建立节点
DOM 添加节点
DOM 克隆节点
DOM 节点类型
DOM Node
DOM NodeList
DOM NamedNodeMap
DOM Document
DOM DocumentType
DOM ProcessingInstr
DOM Element

XML DOM 中的 DOM 访问节点树


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 610 ::
收藏到网摘: n/a

With the DOM, you can access every node in an XML document.
同通过DOM,你可以访问一个XML文档中的所有节点。


Find and Access Nodes
查找和访问节点

You can find the element you want to manipulate in several ways:
你可以通过下述方法查找你所需要操作的元素:

  • By using the getElementsByTagName() method
    通过使用etElementsByTagName()方法
  • By using the parentNode, firstChild, and lastChild properties of an element node
    通过使用一个元素节点中的parentNode[父类节点]、firstChild [子类节点] 和 lastChild[最后一个子类节点]属性。

getElementsByTagName()

The getElementsByTagName() method can find any XML element in the entire document.
通过使用getElementsByTagName()方法可以查找整个文档中的所有XML元素。

This method ignore the document structure. If you want to find all <book> elements in the document, the getElementsByTagName() method will find them all, regardless of on which level the <book> elements are.
这个方法忽略了整个文档结构。如果你希望查找文档中的所有<book>元素,那么你可以使用getElementsByTagName()方法来查找,无视<book>元素的级别。

This method gives you the XML elements you need regardless of where they are in the document!
该方法为你提供了你所需要的XML元素,不管它们位于文档中的哪个位置。

The getElementsByTagName() method returns all elements (as a nodeList) with the specified tag name that are descendants of the element you are on when using this method.
getElementsByTagName()方法(以一个节点列表的形式)返回了指定标签名的所有元素,这些标签名称是该方法所作用的元素的孙类元素。

The getElementsByTagName() can be used on any XML element:
getElementsByTagName()可以用于任何一个XML元素:

getElementsByTagName() Syntax
getElementsByTagName() 语法

getElementsByTagName("tagname");

Example
案例

The following example returns a nodeList of all <book> elements in the document:
下述案例以一个节点列表的形式返回了文档中的所有<book>元素:

xmlDoc.getElementsByTagName("book");

nodeList
节点列表

When working with a nodeList, we usually store the list in a variable, like this:
当运行一个节点列表时,我们通常将该列表存储在一个变量中,如下所示:

var x=xmlDoc.getElementsByTagName("book");

Now the variable x contains a list of all <book> elements in the page, and we can access the <book> elements by their index numbers.
当前情况下,变量 x 所包含的是整个页面内所有<book>元素的列表,我们可以通过与它们对应的索引号来访问这些<book>元素。

Note: The index starts at 0.
注意:索引起始数从0开始。

You can loop through the nodeList by using the length property:
你可以使用长度属性对节点列表进行循环:

var x=xmlDoc.getElementsByTagName("book");
for (var i=0;i<x.length;i++)
{
// do something with each <book> element
}

You can also access a specific element by using the index number.
你也可以使用索引号来访问一个指定的元素。

To access the third <p> you can write:
如果你希望访问第三个<p>元素,你可以以下面这样的方式书写:

var y=x[2];

parentNode, firstChild, and lastChild
parentNode[父类节点]、firstChild[第一个子类元素]和lastChild[最后一个子类元素]

The three properties parentNode, firstChild, and lastChild follow the document structure and allow short-distance travel in the document.
三个属性:parentNode[父类节点]、firstChild [子类节点] 和 lastChild[最后一个子类节点]属性是紧跟文档之后的,并且允许它们在文档中短距离传输。

Look at the following XML fragment:
请看下面这份XML片段:

<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

</bookstore>

In the HTML code above, the <title> element is the firstChild of the <book> element, and the <price> element is the lastChild of the <book> element.
在上述的HTML代码中,<title>元素是<book>元素的firstChild[第一个子元素],<price>元素是<book>元素的lastChild[最后一个子元素]。

Furthermore, the <book> element is the parentNode of the <title>, <author>, <year>, and <price> elements.
再者,<book>元素是<title>、<author>、<year> 和 <price> 元素的父类元素。


Root Nodes
根节点

There is one special document property that allow access to the tags:
这里包含一个特定的允许访问标签的文档属性:

  • document.documentElement

This property returns the root node of the document and exists in all XML and HTML documents.
该属性返回了文档和存在于所有XML和HTML文档内的根节点。

评论 (0) All

登陆 | 还没注册?