Path: blob/master/src/java.xml/share/classes/org/w3c/dom/Element.java
40948 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* This file is available under and governed by the GNU General Public26* License version 2 only, as published by the Free Software Foundation.27* However, the following notice accompanied the original version of this28* file and, per its terms, should not be removed:29*30* Copyright (c) 2004 World Wide Web Consortium,31*32* (Massachusetts Institute of Technology, European Research Consortium for33* Informatics and Mathematics, Keio University). All Rights Reserved. This34* work is distributed under the W3C(r) Software License [1] in the hope that35* it will be useful, but WITHOUT ANY WARRANTY; without even the implied36* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.37*38* [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-2002123139*/4041package org.w3c.dom;4243/**44* The <code>Element</code> interface represents an element in an HTML or XML45* document. Elements may have attributes associated with them; since the46* <code>Element</code> interface inherits from <code>Node</code>, the47* generic <code>Node</code> interface attribute <code>attributes</code> may48* be used to retrieve the set of all attributes for an element. There are49* methods on the <code>Element</code> interface to retrieve either an50* <code>Attr</code> object by name or an attribute value by name. In XML,51* where an attribute value may contain entity references, an52* <code>Attr</code> object should be retrieved to examine the possibly53* fairly complex sub-tree representing the attribute value. On the other54* hand, in HTML, where all attributes have simple string values, methods to55* directly access an attribute value can safely be used as a convenience.56* <p ><b>Note:</b> In DOM Level 2, the method <code>normalize</code> is57* inherited from the <code>Node</code> interface where it was moved.58* <p>See also the <a href='http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407'>Document Object Model (DOM) Level 3 Core Specification</a>.59*/60public interface Element extends Node {61/**62* The name of the element. If <code>Node.localName</code> is different63* from <code>null</code>, this attribute is a qualified name. For64* example, in:65* <pre> <elementExample id="demo"> ...66* </elementExample> , </pre>67* <code>tagName</code> has the value68* <code>"elementExample"</code>. Note that this is case-preserving in69* XML, as are all of the operations of the DOM. The HTML DOM returns70* the <code>tagName</code> of an HTML element in the canonical71* uppercase form, regardless of the case in the source HTML document.72*/73public String getTagName();7475/**76* Retrieves an attribute value by name.77* @param name The name of the attribute to retrieve.78* @return The <code>Attr</code> value as a string, or the empty string79* if that attribute does not have a specified or default value.80*/81public String getAttribute(String name);8283/**84* Adds a new attribute. If an attribute with that name is already present85* in the element, its value is changed to be that of the value86* parameter. This value is a simple string; it is not parsed as it is87* being set. So any markup (such as syntax to be recognized as an88* entity reference) is treated as literal text, and needs to be89* appropriately escaped by the implementation when it is written out.90* In order to assign an attribute value that contains entity91* references, the user must create an <code>Attr</code> node plus any92* <code>Text</code> and <code>EntityReference</code> nodes, build the93* appropriate subtree, and use <code>setAttributeNode</code> to assign94* it as the value of an attribute.95* <br>To set an attribute with a qualified name and namespace URI, use96* the <code>setAttributeNS</code> method.97* @param name The name of the attribute to create or alter.98* @param value Value to set in string form.99* @exception DOMException100* INVALID_CHARACTER_ERR: Raised if the specified name is not an XML101* name according to the XML version in use specified in the102* <code>Document.xmlVersion</code> attribute.103* <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.104*/105public void setAttribute(String name,106String value)107throws DOMException;108109/**110* Removes an attribute by name. If a default value for the removed111* attribute is defined in the DTD, a new attribute immediately appears112* with the default value as well as the corresponding namespace URI,113* local name, and prefix when applicable. The implementation may handle114* default values from other schemas similarly but applications should115* use <code>Document.normalizeDocument()</code> to guarantee this116* information is up-to-date.117* <br>If no attribute with this name is found, this method has no effect.118* <br>To remove an attribute by local name and namespace URI, use the119* <code>removeAttributeNS</code> method.120* @param name The name of the attribute to remove.121* @exception DOMException122* NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.123*/124public void removeAttribute(String name)125throws DOMException;126127/**128* Retrieves an attribute node by name.129* <br>To retrieve an attribute node by qualified name and namespace URI,130* use the <code>getAttributeNodeNS</code> method.131* @param name The name (<code>nodeName</code>) of the attribute to132* retrieve.133* @return The <code>Attr</code> node with the specified name (134* <code>nodeName</code>) or <code>null</code> if there is no such135* attribute.136*/137public Attr getAttributeNode(String name);138139/**140* Adds a new attribute node. If an attribute with that name (141* <code>nodeName</code>) is already present in the element, it is142* replaced by the new one. Replacing an attribute node by itself has no143* effect.144* <br>To add a new attribute node with a qualified name and namespace145* URI, use the <code>setAttributeNodeNS</code> method.146* @param newAttr The <code>Attr</code> node to add to the attribute list.147* @return If the <code>newAttr</code> attribute replaces an existing148* attribute, the replaced <code>Attr</code> node is returned,149* otherwise <code>null</code> is returned.150* @exception DOMException151* WRONG_DOCUMENT_ERR: Raised if <code>newAttr</code> was created from a152* different document than the one that created the element.153* <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.154* <br>INUSE_ATTRIBUTE_ERR: Raised if <code>newAttr</code> is already an155* attribute of another <code>Element</code> object. The DOM user must156* explicitly clone <code>Attr</code> nodes to re-use them in other157* elements.158*/159public Attr setAttributeNode(Attr newAttr)160throws DOMException;161162/**163* Removes the specified attribute node. If a default value for the164* removed <code>Attr</code> node is defined in the DTD, a new node165* immediately appears with the default value as well as the166* corresponding namespace URI, local name, and prefix when applicable.167* The implementation may handle default values from other schemas168* similarly but applications should use169* <code>Document.normalizeDocument()</code> to guarantee this170* information is up-to-date.171* @param oldAttr The <code>Attr</code> node to remove from the attribute172* list.173* @return The <code>Attr</code> node that was removed.174* @exception DOMException175* NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.176* <br>NOT_FOUND_ERR: Raised if <code>oldAttr</code> is not an attribute177* of the element.178*/179public Attr removeAttributeNode(Attr oldAttr)180throws DOMException;181182/**183* Returns a <code>NodeList</code> of all descendant <code>Elements</code>184* with a given tag name, in document order.185* @param name The name of the tag to match on. The special value "*"186* matches all tags.187* @return A list of matching <code>Element</code> nodes.188*/189public NodeList getElementsByTagName(String name);190191/**192* Retrieves an attribute value by local name and namespace URI.193* <br>Per [<a href='http://www.w3.org/TR/1999/REC-xml-names-19990114/'>XML Namespaces</a>]194* , applications must use the value <code>null</code> as the195* <code>namespaceURI</code> parameter for methods if they wish to have196* no namespace.197* @param namespaceURI The namespace URI of the attribute to retrieve.198* @param localName The local name of the attribute to retrieve.199* @return The <code>Attr</code> value as a string, or the empty string200* if that attribute does not have a specified or default value.201* @exception DOMException202* NOT_SUPPORTED_ERR: May be raised if the implementation does not203* support the feature <code>"XML"</code> and the language exposed204* through the Document does not support XML Namespaces (such as [<a href='http://www.w3.org/TR/1999/REC-html401-19991224/'>HTML 4.01</a>]).205* @since 1.4, DOM Level 2206*/207public String getAttributeNS(String namespaceURI,208String localName)209throws DOMException;210211/**212* Adds a new attribute. If an attribute with the same local name and213* namespace URI is already present on the element, its prefix is214* changed to be the prefix part of the <code>qualifiedName</code>, and215* its value is changed to be the <code>value</code> parameter. This216* value is a simple string; it is not parsed as it is being set. So any217* markup (such as syntax to be recognized as an entity reference) is218* treated as literal text, and needs to be appropriately escaped by the219* implementation when it is written out. In order to assign an220* attribute value that contains entity references, the user must create221* an <code>Attr</code> node plus any <code>Text</code> and222* <code>EntityReference</code> nodes, build the appropriate subtree,223* and use <code>setAttributeNodeNS</code> or224* <code>setAttributeNode</code> to assign it as the value of an225* attribute.226* <br>Per [<a href='http://www.w3.org/TR/1999/REC-xml-names-19990114/'>XML Namespaces</a>]227* , applications must use the value <code>null</code> as the228* <code>namespaceURI</code> parameter for methods if they wish to have229* no namespace.230* @param namespaceURI The namespace URI of the attribute to create or231* alter.232* @param qualifiedName The qualified name of the attribute to create or233* alter.234* @param value The value to set in string form.235* @exception DOMException236* INVALID_CHARACTER_ERR: Raised if the specified qualified name is not237* an XML name according to the XML version in use specified in the238* <code>Document.xmlVersion</code> attribute.239* <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.240* <br>NAMESPACE_ERR: Raised if the <code>qualifiedName</code> is241* malformed per the Namespaces in XML specification, if the242* <code>qualifiedName</code> has a prefix and the243* <code>namespaceURI</code> is <code>null</code>, if the244* <code>qualifiedName</code> has a prefix that is "xml" and the245* <code>namespaceURI</code> is different from "<a href='http://www.w3.org/XML/1998/namespace'>246* http://www.w3.org/XML/1998/namespace</a>", if the <code>qualifiedName</code> or its prefix is "xmlns" and the247* <code>namespaceURI</code> is different from "<a href='http://www.w3.org/2000/xmlns/'>http://www.w3.org/2000/xmlns/</a>", or if the <code>namespaceURI</code> is "<a href='http://www.w3.org/2000/xmlns/'>http://www.w3.org/2000/xmlns/</a>" and neither the <code>qualifiedName</code> nor its prefix is "xmlns".248* <br>NOT_SUPPORTED_ERR: May be raised if the implementation does not249* support the feature <code>"XML"</code> and the language exposed250* through the Document does not support XML Namespaces (such as [<a href='http://www.w3.org/TR/1999/REC-html401-19991224/'>HTML 4.01</a>]).251* @since 1.4, DOM Level 2252*/253public void setAttributeNS(String namespaceURI,254String qualifiedName,255String value)256throws DOMException;257258/**259* Removes an attribute by local name and namespace URI. If a default260* value for the removed attribute is defined in the DTD, a new261* attribute immediately appears with the default value as well as the262* corresponding namespace URI, local name, and prefix when applicable.263* The implementation may handle default values from other schemas264* similarly but applications should use265* <code>Document.normalizeDocument()</code> to guarantee this266* information is up-to-date.267* <br>If no attribute with this local name and namespace URI is found,268* this method has no effect.269* <br>Per [<a href='http://www.w3.org/TR/1999/REC-xml-names-19990114/'>XML Namespaces</a>]270* , applications must use the value <code>null</code> as the271* <code>namespaceURI</code> parameter for methods if they wish to have272* no namespace.273* @param namespaceURI The namespace URI of the attribute to remove.274* @param localName The local name of the attribute to remove.275* @exception DOMException276* NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.277* <br>NOT_SUPPORTED_ERR: May be raised if the implementation does not278* support the feature <code>"XML"</code> and the language exposed279* through the Document does not support XML Namespaces (such as [<a href='http://www.w3.org/TR/1999/REC-html401-19991224/'>HTML 4.01</a>]).280* @since 1.4, DOM Level 2281*/282public void removeAttributeNS(String namespaceURI,283String localName)284throws DOMException;285286/**287* Retrieves an <code>Attr</code> node by local name and namespace URI.288* <br>Per [<a href='http://www.w3.org/TR/1999/REC-xml-names-19990114/'>XML Namespaces</a>]289* , applications must use the value <code>null</code> as the290* <code>namespaceURI</code> parameter for methods if they wish to have291* no namespace.292* @param namespaceURI The namespace URI of the attribute to retrieve.293* @param localName The local name of the attribute to retrieve.294* @return The <code>Attr</code> node with the specified attribute local295* name and namespace URI or <code>null</code> if there is no such296* attribute.297* @exception DOMException298* NOT_SUPPORTED_ERR: May be raised if the implementation does not299* support the feature <code>"XML"</code> and the language exposed300* through the Document does not support XML Namespaces (such as [<a href='http://www.w3.org/TR/1999/REC-html401-19991224/'>HTML 4.01</a>]).301* @since 1.4, DOM Level 2302*/303public Attr getAttributeNodeNS(String namespaceURI,304String localName)305throws DOMException;306307/**308* Adds a new attribute. If an attribute with that local name and that309* namespace URI is already present in the element, it is replaced by310* the new one. Replacing an attribute node by itself has no effect.311* <br>Per [<a href='http://www.w3.org/TR/1999/REC-xml-names-19990114/'>XML Namespaces</a>]312* , applications must use the value <code>null</code> as the313* <code>namespaceURI</code> parameter for methods if they wish to have314* no namespace.315* @param newAttr The <code>Attr</code> node to add to the attribute list.316* @return If the <code>newAttr</code> attribute replaces an existing317* attribute with the same local name and namespace URI, the replaced318* <code>Attr</code> node is returned, otherwise <code>null</code> is319* returned.320* @exception DOMException321* WRONG_DOCUMENT_ERR: Raised if <code>newAttr</code> was created from a322* different document than the one that created the element.323* <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.324* <br>INUSE_ATTRIBUTE_ERR: Raised if <code>newAttr</code> is already an325* attribute of another <code>Element</code> object. The DOM user must326* explicitly clone <code>Attr</code> nodes to re-use them in other327* elements.328* <br>NOT_SUPPORTED_ERR: May be raised if the implementation does not329* support the feature <code>"XML"</code> and the language exposed330* through the Document does not support XML Namespaces (such as [<a href='http://www.w3.org/TR/1999/REC-html401-19991224/'>HTML 4.01</a>]).331* @since 1.4, DOM Level 2332*/333public Attr setAttributeNodeNS(Attr newAttr)334throws DOMException;335336/**337* Returns a <code>NodeList</code> of all the descendant338* <code>Elements</code> with a given local name and namespace URI in339* document order.340* @param namespaceURI The namespace URI of the elements to match on. The341* special value "*" matches all namespaces.342* @param localName The local name of the elements to match on. The343* special value "*" matches all local names.344* @return A new <code>NodeList</code> object containing all the matched345* <code>Elements</code>.346* @exception DOMException347* NOT_SUPPORTED_ERR: May be raised if the implementation does not348* support the feature <code>"XML"</code> and the language exposed349* through the Document does not support XML Namespaces (such as [<a href='http://www.w3.org/TR/1999/REC-html401-19991224/'>HTML 4.01</a>]).350* @since 1.4, DOM Level 2351*/352public NodeList getElementsByTagNameNS(String namespaceURI,353String localName)354throws DOMException;355356/**357* Returns <code>true</code> when an attribute with a given name is358* specified on this element or has a default value, <code>false</code>359* otherwise.360* @param name The name of the attribute to look for.361* @return <code>true</code> if an attribute with the given name is362* specified on this element or has a default value, <code>false</code>363* otherwise.364* @since 1.4, DOM Level 2365*/366public boolean hasAttribute(String name);367368/**369* Returns <code>true</code> when an attribute with a given local name and370* namespace URI is specified on this element or has a default value,371* <code>false</code> otherwise.372* <br>Per [<a href='http://www.w3.org/TR/1999/REC-xml-names-19990114/'>XML Namespaces</a>]373* , applications must use the value <code>null</code> as the374* <code>namespaceURI</code> parameter for methods if they wish to have375* no namespace.376* @param namespaceURI The namespace URI of the attribute to look for.377* @param localName The local name of the attribute to look for.378* @return <code>true</code> if an attribute with the given local name379* and namespace URI is specified or has a default value on this380* element, <code>false</code> otherwise.381* @exception DOMException382* NOT_SUPPORTED_ERR: May be raised if the implementation does not383* support the feature <code>"XML"</code> and the language exposed384* through the Document does not support XML Namespaces (such as [<a href='http://www.w3.org/TR/1999/REC-html401-19991224/'>HTML 4.01</a>]).385* @since 1.4, DOM Level 2386*/387public boolean hasAttributeNS(String namespaceURI,388String localName)389throws DOMException;390391/**392* The type information associated with this element.393* @since 1.5, DOM Level 3394*/395public TypeInfo getSchemaTypeInfo();396397/**398* If the parameter <code>isId</code> is <code>true</code>, this method399* declares the specified attribute to be a user-determined ID attribute400* . This affects the value of <code>Attr.isId</code> and the behavior401* of <code>Document.getElementById</code>, but does not change any402* schema that may be in use, in particular this does not affect the403* <code>Attr.schemaTypeInfo</code> of the specified <code>Attr</code>404* node. Use the value <code>false</code> for the parameter405* <code>isId</code> to undeclare an attribute for being a406* user-determined ID attribute.407* <br> To specify an attribute by local name and namespace URI, use the408* <code>setIdAttributeNS</code> method.409* @param name The name of the attribute.410* @param isId Whether the attribute is a of type ID.411* @exception DOMException412* NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.413* <br>NOT_FOUND_ERR: Raised if the specified node is not an attribute414* of this element.415* @since 1.5, DOM Level 3416*/417public void setIdAttribute(String name,418boolean isId)419throws DOMException;420421/**422* If the parameter <code>isId</code> is <code>true</code>, this method423* declares the specified attribute to be a user-determined ID attribute424* . This affects the value of <code>Attr.isId</code> and the behavior425* of <code>Document.getElementById</code>, but does not change any426* schema that may be in use, in particular this does not affect the427* <code>Attr.schemaTypeInfo</code> of the specified <code>Attr</code>428* node. Use the value <code>false</code> for the parameter429* <code>isId</code> to undeclare an attribute for being a430* user-determined ID attribute.431* @param namespaceURI The namespace URI of the attribute.432* @param localName The local name of the attribute.433* @param isId Whether the attribute is a of type ID.434* @exception DOMException435* NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.436* <br>NOT_FOUND_ERR: Raised if the specified node is not an attribute437* of this element.438* @since 1.5, DOM Level 3439*/440public void setIdAttributeNS(String namespaceURI,441String localName,442boolean isId)443throws DOMException;444445/**446* If the parameter <code>isId</code> is <code>true</code>, this method447* declares the specified attribute to be a user-determined ID attribute448* . This affects the value of <code>Attr.isId</code> and the behavior449* of <code>Document.getElementById</code>, but does not change any450* schema that may be in use, in particular this does not affect the451* <code>Attr.schemaTypeInfo</code> of the specified <code>Attr</code>452* node. Use the value <code>false</code> for the parameter453* <code>isId</code> to undeclare an attribute for being a454* user-determined ID attribute.455* @param idAttr The attribute node.456* @param isId Whether the attribute is a of type ID.457* @exception DOMException458* NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.459* <br>NOT_FOUND_ERR: Raised if the specified node is not an attribute460* of this element.461* @since 1.5, DOM Level 3462*/463public void setIdAttributeNode(Attr idAttr,464boolean isId)465throws DOMException;466467}468469470