XML::DOM::Node(3)
π¦ NAME π¦
okay so this is the XML::DOM::Node(3) manual page, and bestie, XML::DOM::Node is literally the super class of all nodes in XML::DOM. main character energy, no cap. π
πΈ DESCRIPTION πΈ
XML::DOM::Node is the super class of all nodes in an XML::DOM document. The way that works is every node that subclasses XML::DOM::Node also inherits all the methods that XML::DOM::Node implements. It's giving iconic inheritance behavior, honestly. β¨
π«Ά GLOBAL VARIABLES π«Ά
@NodeNamesβ lowkey important: the variable@XML::DOM::Node::NodeNamesmaps the node type constants to strings. It is used byXML::DOM::Node::getNodeTypeName. periodt. π
π METHODS π
getNodeTypeβ returns an integer indicating the node type. Slay. SeeXML::DOMconstants. βgetNodeNameβ returns a property or a hardcoded string, depending on the node type. Here are the corresponding functions or values:AttrβgetNameAttDefβgetNameAttlistDeclβgetNameCDATASectionβ"#cdata-section"Commentβ"#comment"Documentβ"#document"DocumentTypeβgetNodeNameDocumentFragmentβ"#document-fragment"ElementβgetTagNameElementDeclβgetNameEntityReferenceβgetEntityNameEntityβgetNotationNameNotationβgetNameProcessingInstructionβgetTargetTextβ"#text"XMLDeclβ"#xml-declaration"
This mapping ate, fr fr. π
Not In DOM Spec: AttDef, AttlistDecl, ElementDecl and XMLDecl were added for completeness.
getNodeValueandsetNodeValue (value)β returns a string orundef, depending on the node type. This method is provided for completeness. In other languages it saves the programmer an upcast. The value is either available thru some other method defined in the subclass, or elseundefis returned. Here are the corresponding methods:Attr::getValue,Text::getData,CDATASection::getData,Comment::getData,ProcessingInstruction::getData. πgetParentNodeandsetParentNode (parentNode)β the parent of this node. All nodes, exceptDocument,DocumentFragment, andAttrmay have a parent. However, if a node has just been created and not yet added to the tree, or if it has been removed from the tree, this isundef. π«ΆgetChildNodesβ aNodeListthat contains all children of this node. If there are no children, this is aNodeListcontaining no nodes. The content of the returnedNodeListis "live" in the sense that, for instance, changes to the children of the node object that it was created from are immediately reflected in the nodes returned by theNodeListaccessors; it is not a static snapshot of the content of the node. This is highkey true for everyNodeList, including the ones returned by thegetElementsByTagNamemethod. π
NOTE: ngl, this implementation does not return a "live"NodeListforgetElementsByTagName. See CAVEATS.
When this method is called in a list context, it returns a regular perl list containing the child nodes. Note that this list is not "live". E.g.
@list = $node->getChildNodes; # returns a perl list
$nodelist = $node->getChildNodes; # returns a NodeList (object reference)
for my $kid ($node->getChildNodes) # iterate over the children of $node
getFirstChildβ the first child of this node. If there is no such node, this returnsundef. πΈgetLastChildβ the last child of this node. If there is no such node, this returnsundef. πgetPreviousSiblingβ the node immediately preceding this node. If there is no such node, this returnsundef. πgetNextSiblingβ the node immediately following this node. If there is no such node, this returnsundef. π¦getAttributesβ aNamedNodeMapcontaining the attributes (Attrnodes) of this node (if it is anElement) orundefotherwise. Note that adding/removing attributes from the returned object also adds/removes attributes from theElementnode that theNamedNodeMapcame from. Not me being obsessed with that two-way connection. πgetOwnerDocumentβ theDocumentobject associated with this node. This is also theDocumentobject used to create new nodes. When this node is aDocumentthis isundef. iykyk. πinsertBefore (newChild, refChild)β inserts the nodenewChildbefore the existing child noderefChild. IfrefChildisundef, insertnewChildat the end of the list of children. π«Ά
If newChild is a DocumentFragment object, all of its children are inserted, in the same order, before refChild. If the newChild is already in the tree, it is first removed.
Return Value: The node being inserted.
DOMExceptions:
HIERARCHY_REQUEST_ERRβ raised if this node is of a type that does not allow children of the type of thenewChildnode, or if the node to insert is one of this node's ancestors.WRONG_DOCUMENT_ERRβ raised ifnewChildwas created from a different document than the one that created this node.NO_MODIFICATION_ALLOWED_ERRβ raised if this node is readonly.NOT_FOUND_ERRβ raised ifrefChildis not a child of this node.
replaceChild (newChild, oldChild)β replaces the child nodeoldChildwithnewChildin the list of children, and returns theoldChildnode. If thenewChildis already in the tree, it is first removed. π₯
Return Value: The node replaced.
DOMExceptions:
HIERARCHY_REQUEST_ERRβ raised if this node is of a type that does not allow children of the type of thenewChildnode, or if the node to put in is one of this node's ancestors.WRONG_DOCUMENT_ERRβ raised ifnewChildwas created from a different document than the one that created this node.NO_MODIFICATION_ALLOWED_ERRβ raised if this node is readonly.NOT_FOUND_ERRβ raised ifoldChildis not a child of this node.
removeChild (oldChild)β removes the child node indicated byoldChildfrom the list of children, and returns it. π
Return Value: The node removed.
DOMExceptions:
NO_MODIFICATION_ALLOWED_ERRβ raised if this node is readonly.NOT_FOUND_ERRβ raised ifoldChildis not a child of this node.
appendChild (newChild)β adds the nodenewChildto the end of the list of children of this node. If thenewChildis already in the tree, it is first removed. If it is aDocumentFragmentobject, the entire contents of the document fragment are moved into the child list of this node. π
Return Value: The node added.
DOMExceptions:
HIERARCHY_REQUEST_ERRβ raised if this node is of a type that does not allow children of the type of thenewChildnode, or if the node to append is one of this node's ancestors.WRONG_DOCUMENT_ERRβ raised ifnewChildwas created from a different document than the one that created this node.NO_MODIFICATION_ALLOWED_ERRβ raised if this node is readonly.
hasChildNodesβ this is a convenience method to allow easy determination of whether a node has any children. Vibe check: does it have kids or not? π§
Return Value: 1 if the node has any children, 0 otherwise.
cloneNode (deep)β returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes. The duplicate node has no parent (parentNodereturnsundef.). π
Cloning an Element copies all attributes and their values, including those generated by the XML processor to represent defaulted attributes, but this method does not copy any text it contains unless it is a deep clone, since the text is contained in a child Text node. Cloning any other type of node simply returns a copy of this node.
Parameters:
deepβ if true, recursively clone the subtree under the specified node. If false, clone only the node itself (and its attributes, if it is anElement).
Return Value: The duplicate node.
normalizeβ puts allTextnodes in the full depth of the sub-tree underneath thisElementinto a "normal" form where only markup (e.g., tags, comments, processing instructions, CDATA sections, and entity references) separatesTextnodes, i.e., there are no adjacentTextnodes. This can be used to ensure that the DOM view of a document is the same as if it were saved and re-loaded, and is useful when operations (such as XPointer lookups) that depend on a particular document tree structure are to be used. β¨
Not In DOM Spec: In the DOM Spec this method is defined in theElementandDocumentclass interfaces only, but it doesn't hurt to have it here...
getElementsByTagName (name [, recurse])β returns aNodeListof all descendant elements with a given tag name, in the order in which they would be encountered in a preorder traversal of theElementtree. π
Parameters:
nameβ the name of the tag to match on. The special value"*"matches all tags.recurseβ whether it should return only direct child nodes (0) or any descendant that matches the tag name (1). This argument is optional and defaults to1. It is not part of the DOM spec.
Return Value: A list of matching Element nodes.
NOTE: this implementation does not return a "live"NodeListforgetElementsByTagName. See CAVEATS.
When this method is called in a list context, it returns a regular perl list containing the result nodes. E.g.
@list = $node->getElementsByTagName("tag"); # returns a perl list
$nodelist = $node->getElementsByTagName("tag"); # returns a NodeList (object ref.)
for my $elem ($node->getElementsByTagName("tag")) # iterate over the result nodes
π Additional methods not in the DOM Spec π
getNodeTypeNameβ returns the string describing the node type. E.g. returns"ELEMENT_NODE"ifgetNodeTypereturnsELEMENT_NODE. It uses@XML::DOM::Node::NodeNames. Periodt. πtoStringβ returns the entire subtree as a string. πprintToFile (filename)β prints the entire subtree to the file with the specified filename. π¨οΈ
Croaks: if the file could not be opened for writing.
printToFileHandle (handle)β prints the entire subtree to the file handle. E.g. to print toSTDOUT:
$node->printToFileHandle (\*STDOUT);
print (obj)β prints the entire subtree using the object's print method. E.g. to print to aFileHandleobject:
$f = new FileHandle ("file.out", "w");
$node->print ($f);
πΈ getChildIndex (child)
Returns the index of child in the list you get from getChildNodes, bestie. Thatβs it, thatβs the vibe.
Return Value: the index, or -1 if the node is literally not found π.
π¦ getChildAtIndex (index)
Hands you the child node at the specified index β or undef if thereβs nothing cute at that spot π.
π addText (text)
Appends the specified text string to the last child if that last child is a Text node. If it isnβt, it appends a brand-new Text node with the specified text instead. Slay.
Return Value: the last child if it was a Text node, or else the new Text node π.
π
dispose
Removes all circular references in this node and its descendants so the objects can be claimed for garbage collection. Iconic cleanup behavior.
β οΈ The objects should not be used afterwards. Like, after dispose, donβt touch them β theyβre donezo π.
β¨ setOwnerDocument (doc)
Sets the ownerDocument property of this node and all its children (and attributes etc.) to the specified doc. This lets you cut and paste document subtrees between different XML::DOM::Documents. Very main-character energy for moving nodes around π«Ά.
β οΈ The node should be removed from the original document first, before calling setOwnerDocument.
This method does nothing when called on a Document node. No cap.
π isAncestor (parent)
Returns 1 if parent is an ancestor of this node, or if it is this node itself. So yes, a node counts as its own ancestor here β the method said what it said π
.
π expandEntityRefs (str)
Expands all the entity references in the string and returns the result. The entity references can be:
- character references, e.g.
{orῂ - default entity references:
",>,<,'and& - entity references defined in
Entityobjects as part of theDocumentTypeof the owningDocument
Character references are expanded into UTF-8. Parameter entity references (e.g. %ent;) are not expanded. Not me getting emotional about entity expansion π.
π§ to_sax ( %HANDLERS )
E.g.
$node->to_sax (DocumentHandler => $my_handler,
Handler => $handler2 );
%HANDLERS may contain the following handlers:
DocumentHandlerDTDHandlerEntityResolverHandlerβ default handler when one of the above is not specified
Each XML::DOM::Node generates the appropriate SAX callbacks (for the appropriate SAX handler). Different SAX handlers can be plugged in to accomplish different things, e.g.:
XML::Checkerwould check the node (currently onlyDocumentandElementnodes are supported)XML::Handler::BuildDOMwould create a new DOM subtree (thereby, in essence, copying theNode)- and in the near future,
XML::Writercould print the node
All Perl SAX related work is still in flux, so this interface may change a little. Lowkey keep that in mind πΈ.
See PerlSAX for the description of the SAX interface.
π check ( [$checker] )
See descriptions for check() in XML::DOM::Document and XML::DOM::Element. Thatβs where the actual check deets live, bestie π«Ά.
π xql ( @XQL_OPTIONS )
To use the xql method, you must first use XML::XQL and XML::XQL::DOM. This method is basically a shortcut for:
$query = new XML::XQL::Query ( @XQL_OPTIONS );
return $query->solve ($node);
If the first parameter in @XQL_OPTIONS is the XQL expression, you can leave off the 'Expr' keyword, so:
$node->xql ("doc//elem1[@attr]", @other_options);
is identical to:
$node->xql (Expr => "doc//elem1[@attr]", @other_options);
See XML::XQL::Query for other available XQL_OPTIONS. See XML::XQL and XML::XQL::Tutorial for more info. XQL girlies rise π₯.
π«Ά isHidden ()
Whether the node is hidden. See Hidden Nodes for details. Spooky little hidden-node moment π.
kisses and clean exit codes, bestie π β signing off from perl v5.42.0, dated 2022-05-23, on the XML::DOM::Node(3) page πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-XML-DOM 1.46-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms