![]() |
| XML Tree class XMLTree class constructors my $newroot=$n->newroot(); #create a new root node
#add element to XML tree
my $element_added=$newroot->newchild('element-name');
#create attribute of "element-name" element
my $attribute_added=$element_added->newattribute('name','value');
#add text node to children of "element-name"
my $text_added=$element_added->newtext('string value');
#add PCDATA element
$element_added->newchild('name')->newtext('string'); #chain rule
#copy works as xsl:copy
$element_added->copy($somenode);
#copy_of works as xsl:copy-of
$element_added->copy_of($somenode);XMLTree class methods my $chs=$n->children(); #get node-set of childern nodes of element
my $attrs=$n->attributes(); #get node-set of attributes of element
my $p=$n->parent(); #get parent of element undef is returned if $n is a root node
my $str=$n->string(); #get text data of node
my $name=$n->name(); #get name of node undef is returned for text nodes
#get value of attribute of element ($n is element)
#if not exist undef is returned
my $aval=$n->getAttribute('name-of-it');
$n->type()>=0 #the node is element
$n->type()==-1 #the node is a text node
$n->type()==-2 #the node is an attribute node
#methods for printing $fh is any object that has the method print IO::File is assumed
$n->print_xml($fh); $n->print_html($fh); $n->print_text($fh);Node-set is a reference to array of XMLTree objects. The class was designed for creation of XML documents. XPath::Expression class builds node-sets. my $xp=XPath::Expression->parse('my expression')||die $XPath::Vars::Error; #compile one
my $ns=$xp->evaluate($n); # execute it with respect to the node, $n - current node
#$xp->string($n) execute and convert to string
#$xp->boolean($n) execute and convert to boolean
if(ref($ns)){
#$ns is a node-set, process the result
foreach my $n (@{$ns}){
#code
}
}else{
#$ns is a scalar
} |