public interface Element extends Node
Element接口表示HTML或XML文档中的元素。
元素可以具有与它们相关联的属性;
由于Element接口从继承Node ,通用Node接口属性attributes可以用于检索集合为一个元件的所有属性的。
Element接口上有一些方法可以通过名称检索一个Attr对象,或者按名称检索一个属性值。
在XML中,属性值可能包含实体引用,应Attr对象以检查表示属性值的可能相当复杂的子树。
另一方面,在HTML中,所有属性都具有简单的字符串值,可以方便地使用直接访问属性值的方法。
注意:在DOM级别2,该方法normalize从继承Node界面,在它被移动。
ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_PRECEDING, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE| Modifier and Type | Method and Description |
|---|---|
String |
getAttribute(String name)
按名称检索属性值。
|
Attr |
getAttributeNode(String name)
按名称检索属性节点。
|
Attr |
getAttributeNodeNS(String namespaceURI, String localName)
通过本地名称和命名空间URI
Attr节点。
|
String |
getAttributeNS(String namespaceURI, String localName)
通过本地名称和命名空间URI检索属性值。
|
NodeList |
getElementsByTagName(String name)
返回
NodeList所有子孙的
Elements具有给定标记名称,在文档顺序。
|
NodeList |
getElementsByTagNameNS(String namespaceURI, String localName)
返回
NodeList所有后代的
Elements具有给定本地名称和命名空间URI在文档顺序。
|
TypeInfo |
getSchemaTypeInfo()
与该元素相关联的类型信息。
|
String |
getTagName()
元素的名称。
|
boolean |
hasAttribute(String name)
返回
true在此元素上被指定给定名称的属性或具有默认值,
false其他。
|
boolean |
hasAttributeNS(String namespaceURI, String localName)
返回
true在此元素上被指定或具有默认值,与给定的本地名称和命名空间的属性URI
false其他。
|
void |
removeAttribute(String name)
按名称删除属性。
|
Attr |
removeAttributeNode(Attr oldAttr)
删除指定的属性节点。
|
void |
removeAttributeNS(String namespaceURI, String localName)
通过本地名称和命名空间URI删除属性。
|
void |
setAttribute(String name, String value)
添加一个新属性。
|
Attr |
setAttributeNode(Attr newAttr)
添加一个新的属性节点。
|
Attr |
setAttributeNodeNS(Attr newAttr)
添加一个新属性。
|
void |
setAttributeNS(String namespaceURI, String qualifiedName, String value)
添加一个新属性。
|
void |
setIdAttribute(String name, boolean isId)
如果参数
isId为
true ,则此方法将指定的属性声明为用户确定的ID属性。
|
void |
setIdAttributeNode(Attr idAttr, boolean isId)
如果参数
isId为
true ,则此方法将指定的属性声明为用户确定的ID属性。
|
void |
setIdAttributeNS(String namespaceURI, String localName, boolean isId)
如果参数
isId为
true ,则此方法将指定的属性声明为用户确定的ID属性。
|
appendChild, cloneNode, compareDocumentPosition, getAttributes, getBaseURI, getChildNodes, getFeature, getFirstChild, getLastChild, getLocalName, getNamespaceURI, getNextSibling, getNodeName, getNodeType, getNodeValue, getOwnerDocument, getParentNode, getPrefix, getPreviousSibling, getTextContent, getUserData, hasAttributes, hasChildNodes, insertBefore, isDefaultNamespace, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, normalize, removeChild, replaceChild, setNodeValue, setPrefix, setTextContent, setUserDataString getTagName()
Node.localName不同于null ,则此属性是限定名称。
例如,在:
<elementExample id="demo"> ...
</elementExample> ,
tagName的值为"elementExample" 。
请注意,这是在XML中的情况保留,DOM的所有操作也是如此。
tagName HTML源文件中的情况如何,HTML DOM tagName以规范的大写形式返回HTML元素的tagName。
String getAttribute(String name)
name - 要检索的属性的名称。
Attr值作为字符串,或空字符串,如果该属性没有指定或默认值。
void setAttribute(String name, String value) throws DOMException
Attr节点加上任何Text和EntityReference节点,构建适当的子树,并使用setAttributeNode将其分配为属性的值。
setAttributeNS方法。
name - 要创建或更改的属性的名称。
value - 以字符串形式设置的值。
DOMException - INVALID_CHARACTER_ERR:如果指定的名称是不按照规定使用的XML版本的XML名称募集Document.xmlVersion属性。
void removeAttribute(String name) throws DOMException
Document.normalizeDocument()来保证此信息是最新的。
removeAttributeNS方法。
name - 要删除的属性的名称。
DOMException - NO_MODIFICATION_ALLOWED_ERR:如果此节点是只读的,则提升。
Attr getAttributeNode(String name)
getAttributeNodeNS方法。
name - 要检索的属性的名称(
nodeName )。
nodeName )或
null的
Attr节点,如果没有此类属性。
Attr setAttributeNode(Attr newAttr) throws DOMException
nodeName )已经存在于元素中,则会被新的属性替换。
替换属性节点本身不起作用。
setAttributeNodeNS方法。
newAttr - 要添加到属性列表的
Attr节点。
newAttr属性替换现有属性,替换
Attr节点返回,否则
null返回。
DOMException - WRONG_DOCUMENT_ERR:如果newAttr是从与创建该元素的文档不同的文档创建的,则引发。
newAttr已经是另一个Element对象的属性,则Element 。
DOM用户必须明确克隆Attr节点,以便在其他元素中重新使用它们。
Attr removeAttributeNode(Attr oldAttr) throws DOMException
Attr节点的默认值,则在适用的情况下,将立即显示一个新节点和默认值以及相应的命名空间URI,本地名称和前缀。
实现可能类似地处理来自其他模式的默认值,但应用程序应使用Document.normalizeDocument()来保证此信息是最新的。
oldAttr - 要从属性列表中删除的
Attr节点。
Attr节点。
DOMException - NO_MODIFICATION_ALLOWED_ERR:如果此节点是只读的,则提升。
oldAttr不是元素的属性,则引发。
NodeList getElementsByTagName(String name)
NodeList所有子孙的
Elements具有给定标记名称,在文档顺序。
name - 要匹配的标签的名称。
特殊值“*”匹配所有标签。
Element节点。
String getAttributeNS(String namespaceURI, String localName) throws DOMException
namespaceURI - 要检索的属性的命名空间URI。
localName - 要检索的属性的本地名称。
Attr值作为字符串,或空字符串,如果该属性没有指定或默认值。
DOMException - NOT_SUPPORTED_ERR:如果实现不支持功能
"XML" ,并且通过文档公开的语言不支持XML命名空间(例如[
HTML 4.01 ]),则可能会被引发。
void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException
qualifiedName的前缀部分,并将其值更改为value参数。
这个值是一个简单的字符串;
它不被解析,因为它被设置。
因此,任何标记(例如要被识别为实体引用的语法)被视为文本文本,并且在写出时需要被实现适当地转义。
为了分配包含实体引用的属性值,用户必须创建一个Attr节点加上任何Text和EntityReference节点,构建适当的子树,并使用setAttributeNodeNS或setAttributeNode将其分配为属性的值。
null作为方法的namespaceURI参数,如果他们希望没有命名空间。
namespaceURI - 要创建或更改的属性的命名空间URI。
qualifiedName - 要创建或更改的属性的限定名称。
value - 以字符串形式设置的值。
DOMException - INVALID_CHARACTER_ERR:如果指定的限定名称不是按照规定的使用中的XML版本的XML名称募集Document.xmlVersion属性。
qualifiedName在XML规范每个命名空间畸形,如果qualifiedName有一个前缀, namespaceURI是null ,如果qualifiedName有前缀为“xml”和namespaceURI是“不同http://www.w3.org/XML/1998/namespace ”,如果qualifiedName或其前缀为“xmlns”和namespaceURI距离“不同http://www.w3.org/2000/xmlns/ ”,或者如果namespaceURI是“ http://www.w3.org/2000/xmlns/ ”,而本qualifiedName也不其前缀为“xmlns”。
"XML" ,并且通过文档公开的语言不支持XML命名空间(例如[ HTML 4.01 ]),则可能会被引发。
void removeAttributeNS(String namespaceURI, String localName) throws DOMException
Document.normalizeDocument()来保证此信息是最新的。
null作为方法的namespaceURI参数,如果他们希望没有命名空间。
namespaceURI - 要删除的属性的命名空间URI。
localName - 要删除的属性的本地名称。
DOMException - NO_MODIFICATION_ALLOWED_ERR:如果此节点是只读的,则提升。
"XML" ,并且通过文档公开的语言不支持XML命名空间(例如[ HTML 4.01 ]),则可能会被引发。
Attr getAttributeNodeNS(String namespaceURI, String localName) throws DOMException
namespaceURI - 要检索的属性的命名空间URI。
localName - 要检索的属性的本地名称。
Attr节点或
null如果没有此类属性。
DOMException - NOT_SUPPORTED_ERR:如果实现不支持功能
"XML" ,并且通过文档公开的语言不支持XML命名空间(例如[
HTML 4.01 ]),则可能会被引发。
Attr setAttributeNodeNS(Attr newAttr) throws DOMException
null作为方法的namespaceURI参数,如果他们希望没有命名空间。
newAttr - 要添加到属性列表的
Attr节点。
newAttr属性替换具有相同本地名称和名称空间URI的现有属性,则更换
Attr节点返回,否则
null返回。
DOMException - WRONG_DOCUMENT_ERR:如果newAttr是从与创建元素的文档不同的文档创建的,则引发。
newAttr已经是另一个Element对象的属性,则Element 。
DOM用户必须明确克隆Attr节点,以便在其他元素中重新使用它们。
"XML" ,并且通过文档公开的语言不支持XML命名空间(例如[ HTML 4.01 ]),则可能会被引发。
NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException
NodeList所有后代的
Elements具有给定本地名称和命名空间URI在文档顺序。
namespaceURI - 要匹配的元素的命名空间URI。
特殊值“*”匹配所有命名空间。
localName - 要匹配的元素的本地名称。
特殊值“*”与所有本地名称相匹配。
NodeList对象包含所有匹配的
Elements 。
DOMException - NOT_SUPPORTED_ERR:如果实现不支持功能
"XML" ,并且通过文档公开的语言不支持XML命名空间(例如[
HTML 4.01 ]),则可能会被引发。
boolean hasAttribute(String name)
true在此元素上被指定给定名称的属性或具有默认值,
false其他。
name - 要查找的属性的名称。
true如果此元素上被指定具有给定名称的属性或具有缺省值,
false否则。
boolean hasAttributeNS(String namespaceURI, String localName) throws DOMException
true在此元素上被指定或具有默认值,与给定的本地名称和命名空间的属性URI false其他。
null作为方法的namespaceURI参数,如果他们希望没有命名空间。
namespaceURI - 要查找的属性的命名空间URI。
localName - 要查找的属性的本地名称。
true如果指定使用给定本地名称和名称空间URI的属性或具有此元件上的默认值,
false否则。
DOMException - NOT_SUPPORTED_ERR:如果实现不支持功能
"XML" ,并且通过文档公开的语言不支持XML命名空间(例如[
HTML 4.01 ]),则可能会被引发。
TypeInfo getSchemaTypeInfo()
void setIdAttribute(String name, boolean isId) throws DOMException
isId为true ,则此方法将指定的属性声明为用户确定的ID属性。
这会影响价值Attr.isId和行为Document.getElementById ,但不会改变,可能是在使用中的任何模式,特别是这并没有影响到Attr.schemaTypeInfo指定的Attr节点。
使用参数isId的值false取消isId作为用户确定的ID属性的属性。
setIdAttributeNS方法。
name - 属性的名称。
isId - 属性是否为类型ID。
DOMException - NO_MODIFICATION_ALLOWED_ERR:如果此节点是只读的,则引发。
void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException
isId为true ,则此方法将指定的属性声明为用户确定的ID属性。
这会影响价值Attr.isId和行为Document.getElementById ,但不会改变,可能是在使用中的任何模式,特别是这并没有影响到Attr.schemaTypeInfo指定的Attr节点。
使用值false作为参数isId取消声明作为用户确定的ID属性的属性。
namespaceURI - 属性的命名空间URI。
localName -
localName的本地名称。
isId - 属性是否为类型ID。
DOMException - NO_MODIFICATION_ALLOWED_ERR:如果此节点是只读的,则引发。
void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException
isId为true ,则此方法将指定的属性声明为用户确定的ID属性。
这会影响价值Attr.isId和行为Document.getElementById ,但不会改变,可能是在使用中的任何模式,特别是这并没有影响到Attr.schemaTypeInfo指定的Attr节点。
对参数isId使用值false取消isId作为用户确定的ID属性的属性。
idAttr - 属性节点。
isId -
isId是否是类型ID。
DOMException - NO_MODIFICATION_ALLOWED_ERR:如果此节点是只读的,则引发。
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.