{
    "mode": "perldoc",
    "parameter": "XML::LibXML::Element",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/XML%3A%3ALibXML%3A%3AElement/json",
    "generated": "2026-06-10T23:07:53Z",
    "synopsis": "use XML::LibXML;\n# Only methods specific to Element nodes are listed here,\n# see the XML::LibXML::Node manpage for other methods\n$node = XML::LibXML::Element->new( $name );\n$node->setAttribute( $aname, $avalue );\n$node->setAttributeNS( $nsURI, $aname, $avalue );\n$avalue = $node->getAttribute( $aname );\n$avalue = $node->getAttributeNS( $nsURI, $aname );\n$attrnode = $node->getAttributeNode( $aname );\n$attrnode = $node->getAttributeNodeNS( $namespaceURI, $aname );\n$node->removeAttribute( $aname );\n$node->removeAttributeNS( $nsURI, $aname );\n$boolean = $node->hasAttribute( $aname );\n$boolean = $node->hasAttributeNS( $nsURI, $aname );\n@nodes = $node->getChildrenByTagName($tagname);\n@nodes = $node->getChildrenByTagNameNS($nsURI,$tagname);\n@nodes = $node->getChildrenByLocalName($localname);\n@nodes = $node->getElementsByTagName($tagname);\n@nodes = $node->getElementsByTagNameNS($nsURI,$localname);\n@nodes = $node->getElementsByLocalName($localname);\n$node->appendWellBalancedChunk( $chunk );\n$node->appendText( $PCDATA );\n$node->appendTextNode( $PCDATA );\n$node->appendTextChild( $childname , $PCDATA );\n$node->setNamespace( $nsURI , $nsPrefix, $activate );\n$node->setNamespaceDeclURI( $nsPrefix, $newURI );\n$node->setNamespaceDeclPrefix( $oldPrefix, $newPrefix );",
    "sections": {
        "NAME": {
            "content": "XML::LibXML::Element - XML::LibXML Class for Element Nodes\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use XML::LibXML;\n# Only methods specific to Element nodes are listed here,\n# see the XML::LibXML::Node manpage for other methods\n\n$node = XML::LibXML::Element->new( $name );\n$node->setAttribute( $aname, $avalue );\n$node->setAttributeNS( $nsURI, $aname, $avalue );\n$avalue = $node->getAttribute( $aname );\n$avalue = $node->getAttributeNS( $nsURI, $aname );\n$attrnode = $node->getAttributeNode( $aname );\n$attrnode = $node->getAttributeNodeNS( $namespaceURI, $aname );\n$node->removeAttribute( $aname );\n$node->removeAttributeNS( $nsURI, $aname );\n$boolean = $node->hasAttribute( $aname );\n$boolean = $node->hasAttributeNS( $nsURI, $aname );\n@nodes = $node->getChildrenByTagName($tagname);\n@nodes = $node->getChildrenByTagNameNS($nsURI,$tagname);\n@nodes = $node->getChildrenByLocalName($localname);\n@nodes = $node->getElementsByTagName($tagname);\n@nodes = $node->getElementsByTagNameNS($nsURI,$localname);\n@nodes = $node->getElementsByLocalName($localname);\n$node->appendWellBalancedChunk( $chunk );\n$node->appendText( $PCDATA );\n$node->appendTextNode( $PCDATA );\n$node->appendTextChild( $childname , $PCDATA );\n$node->setNamespace( $nsURI , $nsPrefix, $activate );\n$node->setNamespaceDeclURI( $nsPrefix, $newURI );\n$node->setNamespaceDeclPrefix( $oldPrefix, $newPrefix );\n",
            "subsections": []
        },
        "METHODS": {
            "content": "The class inherits from XML::LibXML::Node. The documentation for Inherited methods is not listed\nhere.\n\nMany functions listed here are extensively documented in the DOM Level 3 specification\n(<http://www.w3.org/TR/DOM-Level-3-Core/>). Please refer to the specification for extensive\ndocumentation.\n\nnew\n$node = XML::LibXML::Element->new( $name );\n\nThis function creates a new node unbound to any DOM.\n\nsetAttribute\n$node->setAttribute( $aname, $avalue );\n\nThis method sets or replaces the node's attribute $aname to the value $avalue\n\nsetAttributeNS\n$node->setAttributeNS( $nsURI, $aname, $avalue );\n\nNamespace-aware version of \"setAttribute\", where $nsURI is a namespace URI, $aname is a\nqualified name, and $avalue is the value. The namespace URI may be null (empty or undefined)\nin order to create an attribute which has no namespace.\n\nThe current implementation differs from DOM in the following aspects\n\nIf an attribute with the same local name and namespace URI already exists on the element,\nbut its prefix differs from the prefix of $aname, then this function is supposed to change\nthe prefix (regardless of namespace declarations and possible collisions). However, the\ncurrent implementation does rather the opposite. If a prefix is declared for the namespace\nURI in the scope of the attribute, then the already declared prefix is used, disregarding\nthe prefix specified in $aname. If no prefix is declared for the namespace, the function\ntries to declare the prefix specified in $aname and dies if the prefix is already taken by\nsome other namespace.\n\nAccording to DOM Level 2 specification, this method can also be used to create or modify\nspecial attributes used for declaring XML namespaces (which belong to the namespace\n\"http://www.w3.org/2000/xmlns/\" and have prefix or name \"xmlns\"). This should work since\nversion 1.61, but again the implementation differs from DOM specification in the following:\nif a declaration of the same namespace prefix already exists on the element, then changing\nits value via this method automatically changes the namespace of all elements and attributes\nin its scope. This is because in libxml2 the namespace URI of an element is not static but\nis computed from a pointer to a namespace declaration attribute.\n\ngetAttribute\n$avalue = $node->getAttribute( $aname );\n\nIf $node has an attribute with the name $aname, the value of this attribute will get\nreturned.\n\ngetAttributeNS\n$avalue = $node->getAttributeNS( $nsURI, $aname );\n\nRetrieves an attribute value by local name and namespace URI.\n\ngetAttributeNode\n$attrnode = $node->getAttributeNode( $aname );\n\nRetrieve an attribute node by name. If no attribute with a given name exists, \"undef\" is\nreturned.\n\ngetAttributeNodeNS\n$attrnode = $node->getAttributeNodeNS( $namespaceURI, $aname );\n\nRetrieves an attribute node by local name and namespace URI. If no attribute with a given\nlocalname and namespace exists, \"undef\" is returned.\n\nremoveAttribute\n$node->removeAttribute( $aname );\n\nThe method removes the attribute $aname from the node's attribute list, if the attribute can\nbe found.\n\nremoveAttributeNS\n$node->removeAttributeNS( $nsURI, $aname );\n\nNamespace version of \"removeAttribute\"\n\nhasAttribute\n$boolean = $node->hasAttribute( $aname );\n\nThis function tests if the named attribute is set for the node. If the attribute is\nspecified, TRUE (1) will be returned, otherwise the return value is FALSE (0).\n\nhasAttributeNS\n$boolean = $node->hasAttributeNS( $nsURI, $aname );\n\nnamespace version of \"hasAttribute\"\n\ngetChildrenByTagName\n@nodes = $node->getChildrenByTagName($tagname);\n\nThe function gives direct access to all child elements of the current node with a given\ntagname, where tagname is a qualified name, that is, in case of namespace usage it may\nconsist of a prefix and local name. This function makes things a lot easier if one needs to\nhandle big data sets. A special tagname '*' can be used to match any name.\n\nIf this function is called in SCALAR context, it returns the number of elements found.\n\ngetChildrenByTagNameNS\n@nodes = $node->getChildrenByTagNameNS($nsURI,$tagname);\n\nNamespace version of \"getChildrenByTagName\". A special nsURI '*' matches any namespace URI,\nin which case the function behaves just like \"getChildrenByLocalName\".\n\nIf this function is called in SCALAR context, it returns the number of elements found.\n\ngetChildrenByLocalName\n@nodes = $node->getChildrenByLocalName($localname);\n\nThe function gives direct access to all child elements of the current node with a given\nlocal name. It makes things a lot easier if one needs to handle big data sets. A special\n\"localname\" '*' can be used to match any local name.\n\nIf this function is called in SCALAR context, it returns the number of elements found.\n\ngetElementsByTagName\n@nodes = $node->getElementsByTagName($tagname);\n\nThis function is part of the spec. It fetches all descendants of a node with a given\ntagname, where \"tagname\" is a qualified name, that is, in case of namespace usage it may\nconsist of a prefix and local name. A special \"tagname\" '*' can be used to match any tag\nname.\n\nIn SCALAR context this function returns an XML::LibXML::NodeList object.\n\ngetElementsByTagNameNS\n@nodes = $node->getElementsByTagNameNS($nsURI,$localname);\n\nNamespace version of \"getElementsByTagName\" as found in the DOM spec. A special \"localname\"\n'*' can be used to match any local name and \"nsURI\" '*' can be used to match any namespace\nURI.\n\nIn SCALAR context this function returns an XML::LibXML::NodeList object.\n\ngetElementsByLocalName\n@nodes = $node->getElementsByLocalName($localname);\n\nThis function is not found in the DOM specification. It is a mix of getElementsByTagName and\ngetElementsByTagNameNS. It will fetch all tags matching the given local-name. This allows\none to select tags with the same local name across namespace borders.\n\nIn SCALAR context this function returns an XML::LibXML::NodeList object.\n\nappendWellBalancedChunk\n$node->appendWellBalancedChunk( $chunk );\n\nSometimes it is necessary to append a string coded XML Tree to a node.\n*appendWellBalancedChunk* will do the trick for you. But this is only done if the String is\n\"well-balanced\".\n\n*Note that appendWellBalancedChunk() is only left for compatibility reasons*. Implicitly it\nuses\n\nmy $fragment = $parser->parsebalancedchunk( $chunk );\n$node->appendChild( $fragment );\n\nThis form is more explicit and makes it easier to control the flow of a script.\n\nappendText\n$node->appendText( $PCDATA );\n\nalias for appendTextNode().\n\nappendTextNode\n$node->appendTextNode( $PCDATA );\n\nThis wrapper function lets you add a string directly to an element node.\n\nappendTextChild\n$node->appendTextChild( $childname , $PCDATA );\n\nSomewhat similar with \"appendTextNode\": It lets you set an Element, that contains only a\n\"text node\" directly by specifying the name and the text content.\n\nsetNamespace\n$node->setNamespace( $nsURI , $nsPrefix, $activate );\n\nsetNamespace() allows one to apply a namespace to an element. The function takes three\nparameters: 1. the namespace URI, which is required and the two optional values prefix,\nwhich is the namespace prefix, as it should be used in child elements or attributes as well\nas the additional activate parameter. If prefix is not given, undefined or empty, this\nfunction tries to create a declaration of the default namespace.\n\nThe activate parameter is most useful: If this parameter is set to FALSE (0), a new\nnamespace declaration is simply added to the element while the element's namespace itself is\nnot altered. Nevertheless, activate is set to TRUE (1) on default. In this case the\nnamespace is used as the node's effective namespace. This means the namespace prefix is\nadded to the node name and if there was a namespace already active for the node, it will be\nreplaced (but its declaration is not removed from the document). A new namespace declaration\nis only created if necessary (that is, if the element is already in the scope of a namespace\ndeclaration associating the prefix with the namespace URI, then this declaration is reused).\n\nThe following example may clarify this:\n\nmy $e1 = $doc->createElement(\"bar\");\n$e1->setNamespace(\"http://foobar.org\", \"foo\")\n\nresults\n\n<foo:bar xmlns:foo=\"http://foobar.org\"/>\n\nwhile\n\nmy $e2 = $doc->createElement(\"bar\");\n$e2->setNamespace(\"http://foobar.org\", \"foo\",0)\n\nresults only\n\n<bar xmlns:foo=\"http://foobar.org\"/>\n\nBy using $activate == 0 it is possible to create multiple namespace declarations on a single\nelement.\n\nThe function fails if it is required to create a declaration associating the prefix with the\nnamespace URI but the element already carries a declaration with the same prefix but\ndifferent namespace URI.\n\nsetNamespaceDeclURI\n$node->setNamespaceDeclURI( $nsPrefix, $newURI );\n\nEXPERIMENTAL IN 1.61 !\n\nThis function manipulates directly with an existing namespace declaration on an element. It\ntakes two parameters: the prefix by which it looks up the namespace declaration and a new\nnamespace URI which replaces its previous value.\n\nIt returns 1 if the namespace declaration was found and changed, 0 otherwise.\n\nAll elements and attributes (even those previously unbound from the document) for which the\nnamespace declaration determines their namespace belong to the new namespace after the\nchange.\n\nIf the new URI is undef or empty, the nodes have no namespace and no prefix after the\nchange. Namespace declarations once nulled in this way do not further appear in the\nserialized output (but do remain in the document for internal integrity of libxml2 data\nstructures).\n\nThis function is NOT part of any DOM API.\n\nsetNamespaceDeclPrefix\n$node->setNamespaceDeclPrefix( $oldPrefix, $newPrefix );\n\nEXPERIMENTAL IN 1.61 !\n\nThis function manipulates directly with an existing namespace declaration on an element. It\ntakes two parameters: the old prefix by which it looks up the namespace declaration and a\nnew prefix which is to replace the old one.\n\nThe function dies with an error if the element is in the scope of another declaration whose\nprefix equals to the new prefix, or if the change should result in a declaration with a\nnon-empty prefix but empty namespace URI. Otherwise, it returns 1 if the namespace\ndeclaration was found and changed and 0 if not found.\n\nAll elements and attributes (even those previously unbound from the document) for which the\nnamespace declaration determines their namespace change their prefix to the new value.\n\nIf the new prefix is undef or empty, the namespace declaration becomes a declaration of a\ndefault namespace. The corresponding nodes drop their namespace prefix (but remain in the,\nnow default, namespace). In this case the function fails, if the containing element is in\nthe scope of another default namespace declaration.\n\nThis function is NOT part of any DOM API.\n",
            "subsections": []
        },
        "OVERLOADING": {
            "content": "XML::LibXML::Element overloads hash dereferencing to provide access to the element's attributes.\nFor non-namespaced attributes, the attribute name is the hash key, and the attribute value is\nthe hash value. For namespaced attributes, the hash key is qualified with the namespace URI,\nusing Clark notation.\n\nPerl's \"tied hash\" feature is used, which means that the hash gives you read-write access to the\nelement's attributes. For more information, see XML::LibXML::AttributeHash\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "Matt Sergeant, Christian Glahn, Petr Pajas\n",
            "subsections": []
        },
        "VERSION": {
            "content": "2.0134\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "2001-2007, AxKit.com Ltd.\n\n2002-2006, Christian Glahn.\n\n2006-2009, Petr Pajas.\n",
            "subsections": []
        },
        "LICENSE": {
            "content": "This program is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        }
    },
    "summary": "XML::LibXML::Element - XML::LibXML Class for Element Nodes",
    "flags": [],
    "examples": [],
    "see_also": []
}