XML 中的元素和节点有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/132564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:05:17  来源:igfitidea点击:

What's the difference between an element and a node in XML?

xmlxmlnode

提问by Philip Morton

I'm working in Java with XML and I'm wondering; what's the difference between an element and a node?

我正在使用 XML 使用 Java,我想知道;元素和节点有什么区别?

采纳答案by Benoit

The Node object is the primary data type for the entire DOM.

Node 对象是整个 DOM 的主要数据类型。

A node can be an element node, an attribute node, a text node, or any other of the node types explained in the "Node types" chapter.

节点可以是元素节点、属性节点、文本节点或“节点类型”一章中介绍的任何其他节点类型。

An XML element is everything from (including) the element's start tag to (including) the element's end tag.

XML 元素是从(包括)元素的开始标记到(包括)元素的结束标记的所有内容。

回答by Dimitre Novatchev

Different W3C specifications define different sets of "Node" types.

不同的 W3C 规范定义了不同的“节点”类型集。

Thus, the DOM specdefines the following types of nodes:

因此,DOM 规范定义了以下类型的节点:

  • Document-- Element(maximum of one), ProcessingInstruction, Comment, DocumentType
  • DocumentFragment-- Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference
  • DocumentType-- no children
  • EntityReference-- Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference
  • Element-- Element, Text, Comment, ProcessingInstruction, CDATASection, EntityReference
  • Attr-- Text, EntityReference
  • ProcessingInstruction-- no children
  • Comment-- no children
  • Text-- no children
  • CDATASection-- no children
  • Entity-- Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference
  • Notation-- no children
  • Document-- Element(最多一个), ProcessingInstruction, Comment,DocumentType
  • DocumentFragment-- Element, ProcessingInstruction, Comment, Text, CDATASection,EntityReference
  • DocumentType- 没有小孩
  • EntityReference-- Element, ProcessingInstruction, Comment, Text, CDATASection,EntityReference
  • Element-- Element, Text, Comment, ProcessingInstruction, CDATASection,EntityReference
  • Attr-- Text,EntityReference
  • ProcessingInstruction- 没有小孩
  • Comment- 没有小孩
  • Text- 没有小孩
  • CDATASection- 没有小孩
  • Entity-- Element, ProcessingInstruction, Comment, Text, CDATASection,EntityReference
  • Notation- 没有小孩

The XML Infoset(used by XPath) has a smaller set of nodes:

XML信息集(通过XPath的使用)具有一组更小的节点的:

  • The Document Information Item
  • 文档信息项
  • Element Information Items
  • 元素信息项
  • Attribute Information Items
  • 属性信息项
  • Processing Instruction Information Items
  • 处理指令信息项
  • Unexpanded Entity Reference Information Items
  • 未扩展的实体参考信息项
  • Character Information Items
  • 角色信息项目
  • Comment Information Items
  • 评论信息项
  • The Document Type Declaration Information Item
  • 文件类型声明信息项
  • Unparsed Entity Information Items
  • 未解析的实体信息项
  • Notation Information Items
  • 符号信息项目
  • Namespace Information Items
  • 命名空间信息项
  • XPath has the following Node types:

    XPath 具有以下节点类型:

    • root nodes
    • element nodes
    • text nodes
    • attribute nodes
    • namespace nodes
    • processing instruction nodes
    • comment nodes
    • 根节点
    • 元素节点
    • 文本节点
    • 属性节点
    • 命名空间节点
    • 处理指令节点
    • 评论节点

    The answer to your question"What is the difference between an element and a node" is:

    您的问题元素和节点有什么区别的答案是:

    An element is a type of node. Many other types of nodes exist and serve different purposes.

    An element is a type of node. Many other types of nodes exist and serve different purposes.

    回答by mmaibaum

    A Node is a part of the DOM tree, an Element is a particular type of Node

    节点是 DOM 树的一部分,元素是特定类型的节点

    e.g. <foo> This is Text </foo>

    例如 <foo> This is Text </foo>

    You have a foo Element, (which is also a Node, as Element inherits from Node) and a Text Node 'This is Text', that is a child of the foo Element/Node

    您有一个 foo 元素(它也是一个节点,因为 Element 继承自 Node)和一个文本节点“这是文本”,它是 foo 元素/节点的子节点

    回答by Greg Hewgill

    A node can be a number of different kinds of things: some text, a comment, an element, an entity, etc. An element is a particular kind of node.

    节点可以是多种不同类型的事物:一些文本、评论、元素、实体等。元素是一种特定类型的节点。

    回答by fenomas

    As described in the various XML specifications, an elementis that which consists of a start tag, and end tag, and the content in between, or alternately an empty element tag (which has no content or end tag). In other words, these are all elements:

    如各种 XML规范中所述,anelement是由开始标记和结束标记以及其间的内容组成的,或者是一个空元素标记(没有内容或结束标记)。换句话说,这些都是元素:

    <foo> stuff </foo>
    <foo bar="baz"></foo>
    <foo baz="qux" />
    

    Though you hear "node" used with roughly the same meaning, it has no precise definition per XML specs. It's usually used to refer to nodes of things like DOMs, which may be closely related to XML or use XML for their representation.

    尽管您听到的“节点”具有大致相同的含义,但它没有每个 XML 规范的精确定义。它通常用于指代诸如 DOM 之类的事物的节点,这些节点可能与 XML 密切相关,或者使用 XML 来表示它们。

    回答by Colonel Panic

    An xml document is made of nested elements. An element begins at its opening tagand ends at its closing tag. You're probably seen <body>and </body>in html. Everything between the opening and closing tags is the element's content. If an element is defined by a self-closing tag (eg. <br/>) then its content is empty.

    xml 文档由嵌套元素组成。一个元素从它的开始标签开始,到它的结束标签结束。你可能看到的<body></body>在HTML中。开始和结束标记之间的所有内容都是元素的内容。如果一个元素是由一个自闭合标签(例如<br/>)定义的,那么它的内容是空的。

    Opening tags can also specify attributes, eg. <p class="rant">. In this example the attribute nameis 'class' and its value'rant'.

    开始标签还可以指定属性,例如。<p class="rant">. 在这个例子中,属性名称是“class”,它的值是“rant”。

    The XML language has no such thing as a 'node'. Read the spec, the word doesn't occur.

    XML 语言没有“节点”这样的东西阅读规范,这个词不会出现。

    Some people use the word 'node' informally to mean element, which is confusing because some parsers also give the word a technical meaning (identifying 'text nodes' and 'element nodes'). The exact meaning depends on the parser, so the word is ill-defined unless you state what parser you are using. If you mean element, say 'element'.

    有些人非正式地使用“节点”这个词来表示元素,这是令人困惑的,因为一些解析器还赋予这个词一个技术含义(识别“文本节点”和“元素节点”)。确切的含义取决于解析器,因此除非您说明正在使用的解析器,否则该词的定义不明确。如果您的意思是元素,请说 'element'

    回答by Troels Thomsen

    A node is the base class for both elements and attributes (and basically all other XML representations too).

    节点是元素和属性(以及基本上所有其他 XML 表示)的基类。

    回答by eugensk

    Elementis the only kind of nodethat can have child nodes and attributes.

    元素是唯一一种节点可以有子节点和属性。

    Document also has child nodes, BUT
    no attributes, no text, exactly one child element.

    文档也有子节点,但
    没有属性,没有文本,只有一个子元素。

    回答by Robert Rocha

    A node is definedas:

    一个节点定义为:

    the smallest unit of a valid, complete structure in a document.

    文档中有效完整结构的最小单位。

    or as:

    或作为:

    An object in the tree view that serves as a container to hold related objects.

    树视图中的一个对象,用作保存相关对象的容器。

    Now their are many different kinds of nodes as an elements node, an attribute node etc.

    现在它们有许多不同类型的节点,如元素节点、属性节点等。

    回答by Robert Rocha

    Now i know ,the element is one of node

    现在我知道,元素是节点之一

    All node types in here"http://www.w3schools.com/dom/dom_nodetype.asp"

    这里的所有节点类型“ http://www.w3schools.com/dom/dom_nodetype.asp

    Element is between the start tag and end in the end tag

    元素在开始标记和结束标记之间

    So text node is a node , but not a element.

    所以文本节点是一个节点,但不是一个元素。