# perldoc > XML::LibXML::Element

---
type: CommandReference
command: XML::LibXML::Element
mode: perldoc
section: ''
source: perldoc
---

## Quick Reference

- `$node = XML::LibXML::Element->new($name)` — create new element node
- `$node->setAttribute($aname, $avalue)` — set or replace attribute value
- `$node->setAttributeNS($nsURI, $aname, $avalue)` — namespace-aware attribute set
- `$node->getAttribute($aname)` — retrieve attribute value by name
- `$node->getAttributeNS($nsURI, $aname)` — retrieve attribute by namespace+local name
- `$node->hasAttribute($aname)` — test if attribute exists
- `$node->getChildrenByTagName($tagname)` — get child elements by qualified name
- `$node->getElementsByTagName($tagname)` — get all descendant elements by qualified name
- `$node->appendTextNode($PCDATA)` — add text content to element
- `$node->setNamespace($nsURI, $nsPrefix, $activate)` — apply namespace to element

## Name

XML::LibXML::Element — Perl class for XML element nodes (inherits from XML::LibXML::Node)

## Synopsis

perl
use XML::LibXML;
$node = XML::LibXML::Element->new($name);
$node->setAttribute($aname, $avalue);
$node->setAttributeNS($nsURI, $aname, $avalue);
$avalue = $node->getAttribute($aname);
$avalue = $node->getAttributeNS($nsURI, $aname);
$attrnode = $node->getAttributeNode($aname);
$attrnode = $node->getAttributeNodeNS($namespaceURI, $aname);
$node->removeAttribute($aname);
$node->removeAttributeNS($nsURI, $aname);
$boolean = $node->hasAttribute($aname);
$boolean = $node->hasAttributeNS($nsURI, $aname);
@nodes = $node->getChildrenByTagName($tagname);
@nodes = $node->getChildrenByTagNameNS($nsURI, $tagname);
@nodes = $node->getChildrenByLocalName($localname);
@nodes = $node->getElementsByTagName($tagname);
@nodes = $node->getElementsByTagNameNS($nsURI, $localname);
@nodes = $node->getElementsByLocalName($localname);
$node->appendWellBalancedChunk($chunk);
$node->appendText($PCDATA);
$node->appendTextNode($PCDATA);
$node->appendTextChild($childname, $PCDATA);
$node->setNamespace($nsURI, $nsPrefix, $activate);
$node->setNamespaceDeclURI($nsPrefix, $newURI);
$node->setNamespaceDeclPrefix($oldPrefix, $newPrefix);
## Methods

All methods are inherited from XML::LibXML::Node except those listed here. See the DOM Level 3 specification for full documentation.

- `new($name)` — create a new unbound element node
- `setAttribute($aname, $avalue)` — set attribute value (replaces existing)
- `setAttributeNS($nsURI, $aname, $avalue)` — namespace-aware version; $nsURI may be null for no namespace. Note: behavior differs from DOM in prefix handling (see full docs)
- `getAttribute($aname)` — returns attribute value or undef
- `getAttributeNS($nsURI, $aname)` — returns attribute value by namespace and local name
- `getAttributeNode($aname)` — returns attribute node or undef
- `getAttributeNodeNS($namespaceURI, $aname)` — returns attribute node by namespace and local name
- `removeAttribute($aname)` — remove attribute by name
- `removeAttributeNS($nsURI, $aname)` — namespace-aware remove
- `hasAttribute($aname)` — returns 1 if attribute exists, 0 otherwise
- `hasAttributeNS($nsURI, $aname)` — namespace-aware version
- `getChildrenByTagName($tagname)` — return child elements matching qualified name (use '*' for any). In scalar context returns count
- `getChildrenByTagNameNS($nsURI, $tagname)` — namespace version; '*' for any namespace URI matches any local name
- `getChildrenByLocalName($localname)` — return child elements by local name only; '*' matches any
- `getElementsByTagName($tagname)` — return all descendant elements matching qualified name. In scalar context returns XML::LibXML::NodeList
- `getElementsByTagNameNS($nsURI, $localname)` — namespace version; '*' for wildcards
- `getElementsByLocalName($localname)` — non-DOM method; fetches all descendants with given local name across namespace borders
- `appendWellBalancedChunk($chunk)` — append XML string as balanced chunk (compatibility; use `$parser->parse_balanced_chunk` instead)
- `appendText($PCDATA)` — alias for `appendTextNode`
- `appendTextNode($PCDATA)` — add text content to element
- `appendTextChild($childname, $PCDATA)` — create child element with text node
- `setNamespace($nsURI, $nsPrefix, $activate)` — apply namespace to element. If $activate is true (default), namespace becomes effective; if false, only declaration is added. See examples in full docs
- `setNamespaceDeclURI($nsPrefix, $newURI)` — (experimental) change namespace URI of an existing namespace declaration; returns 1 if changed, 0 if not found
- `setNamespaceDeclPrefix($oldPrefix, $newPrefix)` — (experimental) rename prefix of an existing namespace declaration; dies on conflicts

## Overloading

Hash dereferencing is overloaded for attribute access. Non-namespaced attributes use plain key; namespaced attributes use Clark notation (namespace URI in braces). Read-write access via tied hash. See XML::LibXML::AttributeHash.

## See Also

- [XML::LibXML](http://localhost/phpMan.php/perldoc/XML%3A%3ALibXML/markdown)
- [XML::LibXML::Node](http://localhost/phpMan.php/perldoc/XML%3A%3ALibXML%3A%3ANode/markdown)
- [XML::LibXML::NodeList](http://localhost/phpMan.php/perldoc/XML%3A%3ALibXML%3A%3ANodeList/markdown)
- [XML::LibXML::AttributeHash](http://localhost/phpMan.php/perldoc/XML%3A%3ALibXML%3A%3AAttributeHash/markdown)
- DOM Level 3 specification: <http://www.w3.org/TR/DOM-Level-3-Core/>