XML::DOM::Lite 0.10 review
DownloadXML::DOM::Lite is a Lite Pure Perl XML DOM Parser Kit. SYNOPSIS # Parser use XML::DOM::Lite qw(Parser :constants); $pa
|
|
XML::DOM::Lite is a Lite Pure Perl XML DOM Parser Kit.
SYNOPSIS
# Parser
use XML::DOM::Lite qw(Parser :constants);
$parser = Parser->new( %options );
$doc = Parser->parse($xmlstr);
$doc = Parser->parseFile('/path/to/file.xml');
# strip whitespace (can be about 30% faster)
$doc = Parser->parse($xml, whitespace => 'strip');
# All Nodes
$copy = $node->cloneNode($deep);
$nodeType = $node->nodeType;
$parent = $node->parentNode;
$name = $node->nodeName;
$xmlstr = $node->xml;
$owner = $node->ownerDocument;
# Element Nodes
$first = $node->firstChild;
$last = $node->lastChild;
$tag = $node->tagName;
$prev = $node->nextSibling;
$next = $node->previousSibling;
$node->setAttribute("foo", $bar);
$foo = $node->getAttribute("foo");
foreach my $attr (@{$node->attributes}) { # attributes as nodelist
# ... do stuff
}
$node->attributes->{foo} = "bar"; # or as hashref (overload)
$liveNodeList = $node->getElementsByTagName("child"); # deep
$node->insertBefore($newchild, $refchild);
$node->replaceChild($newchild, $refchild);
# Text Nodes
$nodeValue = $node->nodeValue;
$node->nodeValue("new text value");
# Processing Instruction Nodes
# CDATA Nodes
# Comments
$data = $node->nodeValue;
# NodeList
$item = $nodeList->item(42);
$index = $nodeList->nodeIndex($node);
$nlist->insertNode($newNode, $index);
$removed = $nlist->removeNode($node);
$length = $nlist->length; # OR scalar(@$nodeList)
# NodeIterator and NodeFilter
use XML::DOM::Lite qw(NodeIterator :constants);
$niter = NodeIterator->new($rootnode, SHOW_ELEMENT, {
acceptNode => sub {
my $n = shift;
if ($n->tagName eq 'wantme') {
return FILTER_ACCEPT;
} elsif ($n->tagName eq 'skipme') {
return FILTER_SKIP;
} else {
return FILTER_REJECT;
}
}
);
while (my $n = $niter->nextNode) {
# do stuff
}
# XSLT
use XML::DOM::Lite qw(Parser XSLT);
$parser = Parser->new( whitespace => 'strip' );
$xsldoc = $parser->parse($xsl);
$xmldoc = $parser->parse($xml);
$output = XSLT->process($xmldoc, $xsldoc);
# XPath
use XML::DOM::Lite qw(XPath);
$result = XPath->evaluate('/path/to/*[@attr="value"]', $contextNode);
# Document
$rootnode = $doc->documentElement;
$nodeWithId = $doc->getElementById("my_node_id");
$textnode = $doc->createTextNode("some text string");
$element = $doc->createElement("myTagName");
$docfrag = $doc->createDocumentFragment();
$xmlstr = $doc->xml;
$nlist = $doc->selectNodes('/xpath/expression');
$node = $doc->selectSingleNode('/xpath/expression');
# Serializer
use XML::DOM::Lite qw(Serializer);
$serializer = Serializer->new;
$xmlout = $serializer->serializeToString($node);
Requirements:
Perl
XML::DOM::Lite 0.10 search tags