XML::LibXML::Node - abstract Base Class DOM-Nodes |
XML::LibXML::Node - abstract Base Class DOM-Nodes
use XML::LibXML
$name = $node->nodeName; $node->setNodeName( $newName ); $bool = $node->isSameNode( $other_node ); $bool = $node->isEqual( $other_node ); $content = $node->nodeValue; $content = $node->textContent; $type = $node->nodeType; $node->unbindNode() $childnode = $node->removeChild( $childnode ) $oldnode = $node->replaceChild( $newNode, $oldNode ) $node->replaceNode($newNode); $childnode = $node->appendChild( $childnode ); $childnode = $node->addChild( $chilnode ); $node = $parent->addNewChild( $nsURI, $name ); $node->addSibling($newNode); $newnode =$node->cloneNode( $deep ) $parentnode = $node->parentNode; $nextnode = $node->nextSibling() $prevnode = $node->previousSibling() $boolean = $node->hasChildNodes(); $childnode = $node->firstChild; $childnode = $node->lastChild; $documentnode = $node->ownerDocument; $node = $node->getOwner; $node->setOwnerDocument( $doc ); $node->insertBefore( $newNode, $refNode ) $node->insertAfter( $newNode, $refNode ) @nodes = $node->findnodes( $xpath_statement ); $result = $node->find( $xpath ); print $node->findvalue( $xpath ); @childnodes = $node->childNodes; $xmlstring = $node->toString($format,$docencoding); $localname = $node->localname; $nameprefix = $node->prefix; $uri = $node->namespaceURI() $boolean = $node->hasAttributes(); @attributelist = $node->attributes(); $URI = $node->lookupNamespaceURI( $prefix ); $prefix = $node->lookupNamespacePrefix( $URI ); $iter = $node->iterator; $node->normalize; @nslist = $node->getNamespaces; $node->removeChildNodes();
LibXML::Node defines functions that are common to all Node Types. A LibXML::Node should never be created standalone, but as an instance of a high level class such as LibXML::Element or LibXML::Text. The class itself should provide only common functionality. In XML::LibXML each node is part either of a document or a document-fragment. Because of this there is no node without a parent. This may causes confusion with ``unbound'' nodes.
NOTE isEqual will change behaviour to follow the DOM specification
NOTE: Element Nodes have no content per
definition. To get the text value of an Element
use textContent()
instead!
appendChild()
one can use the
addChild()
function. This function is a bit
faster, because it avoids all DOM confomity
checks. Therefore this function is quite usefull
if one builds XML documents in memory where the
order and ownership (ownerDocument) is
ashured.
addChild()
uses libxml2's own xmlAddChild()
function. Thus it has to be used with extra care:
If a text node is added to a node and the node
itself or its last childnode is aswell a text
node, the node to add will be merged with the one
already available. The current node will be
removed from memory after this action. Because
perl is not aware about this action, the perl
instance is still available. XML::LibXML will
catch the loss of a node an avoid to run any
function called on that node.
my $t1 = $doc->createTextNode( "foo" ); my $t2 = $doc->createTextNode( "bar" ); $t1->addChild( $t2 ); # is ok my $val = $t2->nodeValue(); # will fail, script dies
Also addChild()
will not check it the added node
belongs to the same document as the node it will
be added to. This could lead to inconsistent
documents and in more worse cases even to memory
violations, if one does not keep track of this
issue.
Although this sounds like a lot of trouble,
addChild()
is usefull if a document is build from
a stream, such as happens sometimes in SAX
handlers or filters.
If you are not shure about the source of your nodes, you better stay with appendChild(), because this function is more user friendly in the sense of more error tolerance.
addNewChild()
has two parameters $nsURI and $name,
where $nsURI is an (optional) namespace URI. $name
is the fully qualified element name; addNewChild()
will determine the correct prefix if nessecary.
The function returns the newly created node.
This function is very usefull for DOM building, where a created node can be directly associated to its parent. NOTE this function is not part of the DOM specification and its use will limit your code to XML::LibXML.
addSibling()
allows to add an additional node to
the end of a nodelist, defined by the given node.
cloneNode will not copy any namespace information if it is not run recursivly.
This function is the oposite calling of
XML::LibXML::Document's adoptNode()
function. Because of this it has the same
limitations with Entity References as adoptNode().
$node->find( $xpath )->to_literal;
That is, it returns the literal value of the results. This enables you to ensure that you get a string back from your search, allowing certain shortcuts. This could be used as the equivalent of XSLT's .
Additionally to the $format flag of XML::LibXML::Document, this version accepts the optional $docencoding flag. If this flag is set this function returns the string in its original encoding (the encoding of the document) rather than UTF8.
Because XML::LibXML does not implement namespace declarations and attributes the same way, it is required to test what kind of node is handled while accessing the functions result.
If this function is called in array context the attribute nodes are returned as an array. In scalar context the function will return a XML::LibXML::NamedNodeMap object.
NOTE Only the namespace URIs are ment to be unique. The prefix is only document related. also document might has more than a single prefix defined for a namespace.
$node->iterator->iterate( sub { shift;print $_[0]->nodeName(),"\n"; } );
The example will print all node names in the current subtree.
Check the XML::LibXML::Iterator man page for more details.
NOTE: The function has changed with
version 1.53. Earlier versions did not return an
iterator object, but ran the iterate()
function
directly.
xmlTextMerge()
function, since it will not free a
node that is still refered by the perl layer.
Although getNamespaces is available for all nodes, it makes only sense if used with element nodes.
Matt Sergeant, Christian Glahn
XML::LibXML, XML::LibXML::Element, XML::LibXML::Text, XML::LibXML::Comment, XML::LibXML::Attr, XML::LibXML::DocumentFragment
1.53
XML::LibXML::Node - abstract Base Class DOM-Nodes |