DOM节点中属性nodeName、nodeType和nodeValue的区别 Delphi
http://msdn.microsoft.com/zh-cn/library/vstudio/hf9hbf87.aspx
?
<?xml version="1.0"?><books><book><author>Carson</author><price format="dollar">31.95</price><pubdate>05/01/2001</pubdate></book><pubinfo><publisher>MSPress</publisher><state>WA</state></pubinfo></books>?
在 XML 文檔結構中,此圖中的每個圓圈表示一個節點(稱為?XmlNode?對象)。?
XmlNode?對象是 DOM 樹中的基本對象。?
XmlDocument?類(擴展XmlNode)支持用于對整個文檔執行操作(例如,將文檔加載到內存中或將 XML 保存到文件中)的方法。
此外,XmlDocument?提供了查看和處理整個 XML 文檔中的節點的方法。?XmlNode?和?XmlDocument?都具有性能和可用性增強,并通過方法和屬性執行下列操作:
-
訪問和修改 DOM 特定的節點,如元素節點、實體引用節點等。
-
除檢索節點包含的信息(如元素節點中的文本)外,還檢索整個節點。
Node?對象具有一組方法和屬性以及基本的和定義完善的特性。其中的某些特性包括:
-
節點有單個父節點,父節點是與節點相鄰的上一級節點。唯一沒有父級的節點是文檔根,因為它是頂級節點,包含了文檔本身和文檔片段。
-
大多數節點可以有多個子節點,子節點是與節點相鄰的下一級節點。以下是可以有子節點的節點類型列表。
-
Document
-
DocumentFragment
-
EntityReference
-
Element
-
Attribute
XmlDeclaration、Notation、Entity、CDATASection、Text、Comment、ProcessingInstruction?和?DocumentType?節點沒有子節點。
-
-
處于同一級別、在關系圖中由?book?和?pubinfo?節點表示的節點是同級。
DOM 的一個特性是處理屬性的方式。屬性是不屬于父子關系和同級關系的節點。屬性被視為元素節點的屬性,由名稱和值對組成。
例如,如果存在由與元素price?關聯的?format="dollar" 組成的 XML 數據,則單詞?format?是名稱,format?屬性的值是?dollar。
為檢索?price?節點的?format="dollar"?屬性,可以在游標位于?price?元素節點時調用?GetAttribute?方法。有關更多信息,請參見訪問 DOM 中的屬性。
?
?
http://linder0209.iteye.com/blog/1458823
http://www.css88.com/archives/5017
?
?
(一)nodeName?屬性含有某個節點的名稱。
?
元素節點的?nodeName?是標簽名稱
屬性節點的?nodeName?是屬性名稱
文本節點的?nodeName?永遠是?#text
文檔節點的?nodeName?永遠是?#document
注釋:nodeName?所包含的?XML?元素的標簽名稱永遠是 大寫 的
?
nodeName 屬性含有某個節點的名稱。
- 元素(element)節點的 nodeName 是標簽名稱
- 屬性(attribute)節點的 nodeName 是屬性名稱
- 文本(text)節點的 nodeName 永遠是 #text
- 文檔(document)節點的 nodeName 永遠是 #document
?
(二)nodeValue
對于文本節點,nodeValue?屬性包含文本。
對于屬性節點,nodeValue?屬性包含屬性值。
nodeValue?屬性對于文檔節點 <document> 和元素節點 <element> 是不可用的。
?
(三)nodeType
nodeType?屬性可返回節點的類型。
最重要的節點類型是:
元素類型?節點類型
元素element?1
屬性attr?2
文本text?3
注釋comments???8
文檔document???9
?
http://www.w3schools.com/dom/dom_nodetype.asp
Node Types
The following table lists the different W3C node types, and which node types they may have as children:
| Document | Represents the entire document (the root-node of the DOM tree) | Element (max. one), ProcessingInstruction, Comment, DocumentType |
| DocumentFragment | Represents a "lightweight" Document object, which can hold a portion of a document | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
| DocumentType | Provides an interface to the entities defined for the document | None |
| ProcessingInstruction | Represents a processing instruction | None |
| EntityReference | Represents an entity reference | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
| Element | Represents an element | Element, Text, Comment, ProcessingInstruction, CDATASection, EntityReference |
| Attr | Represents an attribute | Text, EntityReference |
| Text | Represents textual content in an element or attribute | None |
| CDATASection | Represents a CDATA section in a document (text that will NOT be parsed by a parser) | None |
| Comment | Represents a comment | None |
| Entity | Represents an entity | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
| Notation | Represents a notation declared in the DTD | None |
Node Types - Return Values
The following table lists what the nodeName and the nodeValue properties will return for each node type:
| Document | #document | null |
| DocumentFragment | #document fragment | null |
| DocumentType | doctype name | null |
| EntityReference | entity reference name | null |
| Element | element name | null |
| Attr | attribute name | attribute value |
| ProcessingInstruction | target | content of node |
| Comment | #comment | comment text |
| Text | #text | content of node |
| CDATASection | #cdata-section | content of node |
| Entity | entity name | null |
| Notation | notation name | null |
NodeTypes - Named Constants
| 1 | ELEMENT_NODE |
| 2 | ATTRIBUTE_NODE |
| 3 | TEXT_NODE |
| 4 | CDATA_SECTION_NODE |
| 5 | ENTITY_REFERENCE_NODE |
| 6 | ENTITY_NODE |
| 7 | PROCESSING_INSTRUCTION_NODE |
| 8 | COMMENT_NODE |
| 9 | DOCUMENT_NODE |
| 10 | DOCUMENT_TYPE_NODE |
| 11 | DOCUMENT_FRAGMENT_NODE |
| 12 | NOTATION_NODE |
?
?http://www.w3school.com.cn/xmldom/dom_nodes_nodelist.asp
<bookstore><book category="children"><title lang="en">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book><book category="cooking"><title lang="en">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</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><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> </bookstore>下面的圖像表示 "books.xml" 中 <title> 元素的節點列表:
?
?
Xml.XMLIntf.IXMLNode.NodeType
Indicates the type of the node.
Read NodeType to determine the type of the node. The type reflects the way the node is used in the XML document.?
TNodeType = (ntReserved, ntElement, ntAttribute, ntText, ntCData,ntEntityRef, ntEntity, ntProcessingInstr, ntComment, ntDocument,ntDocType, ntDocFragment, ntNotation);?
Xml.XMLIntf.IXMLNode.NodeName
Indicates the node name.
NodeName is the name of the underlying DOM node.
The node's name depends on the type of the node, as indicated in the following table:
?
| ntAttribute | The attribute name |
| ntElement | The tag name |
| ntText | '#text' |
| ntCData | '#cdata-section' |
| ntEntityRef | The name of the entity reference. |
| ntEntity | The entity name |
| ntProcessingInstr | the target of the processing instruction |
| ntComment | '#comment' |
| ntDocument | '#document' |
| ntDocType | The document type name |
| ntDocFragment | '#document-fragment' |
| ntNotation | The notation name? |
Xml.XMLIntf.IXMLNode.NodeValue
Specifies the value of this node.
Use NodeValue to get or set the value of the node. The meaning of a node's value depends on the type of the node, as indicated in the following table:?
?
| ntAttribute | The attribute value |
| ntElement | If the element contains only text, this is that text value. Otherwise, trying to read or set NodeValue causes an exception. |
| ntText | The text |
| ntCData | The content of the CDATA section. |
| ntProcessingInstr | The content of the processing instruction except for the target. |
| ntComment | The value (text) of the comment.? |
Xml.XMLIntf.IXMLNode.IsTextElement
Indicates whether the node has a single text value.
Read IsTextElement to determine whether this node represents a single text value. For example, if the node represents the following:?
?
<Title> Understanding XML </Title>IsTextElement is true, because the node represents a tagged element and the text that is its value.
Note:?
In the underlying DOM implementation, nodes for which?IsTextElement?is true are element nodes that have a single child node that is a text node.?
IXMLNode?flattens this out so that you can use the element node to work with the text value (through the?Text?property),
rather than requiring you to use a separate node for the text value.
?
?
總結
以上是生活随笔為你收集整理的DOM节点中属性nodeName、nodeType和nodeValue的区别 Delphi的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小米手环能打电话吗(小米官方售后服务)
- 下一篇: HDS:转型关键还是私有云