Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

xml::parser::lite::tree(3pm) [debian man page]

XML::Parser::Lite::Tree(3pm)				User Contributed Perl Documentation			      XML::Parser::Lite::Tree(3pm)

NAME
XML::Parser::Lite::Tree - Lightweight XML tree builder SYNOPSIS
use XML::Parser::Lite::Tree; my $tree_parser = XML::Parser::Lite::Tree::instance(); my $tree = $tree_parser->parse($xml_data); OR my $tree = XML::Parser::Lite::Tree::instance()->parse($xml_data); DESCRIPTION
This is a singleton class for parsing XML into a tree structure. How does this differ from other XML tree generators? By using XML::Parser::Lite, which is a pure perl XML parser. Using this module you can tree-ify simple XML without having to compile any C. For example, the following XML: <foo woo="yay"><bar a="b" c="d" />hoopla</foo> Parses into the following tree: 'children' => [ { 'children' => [ { 'children' => [], 'attributes' => { 'a' => 'b', 'c' => 'd' }, 'type' => 'element', 'name' => 'bar' }, { 'content' => 'hoopla', 'type' => 'text' } ], 'attributes' => { 'woo' => 'yay' }, 'type' => 'element', 'name' => 'foo' } ], 'type' => 'root' }; Each node contains a "type" key, one of "root", "element" and "text". "root" is the document root, and only contains an array ref "children". "element" represents a normal tag, and contains an array ref "children", a hash ref "attributes" and a string "name". "text" nodes contain only a "content" string. METHODS
"instance()" Returns an instance of the tree parser. "new( options... )" Creates a new parser. Valid options include "process_ns" to process namespaces. "parse($xml)" Parses the xml in $xml and returns the tree as a hash ref. AUTHOR
Copyright (C) 2004-2008, Cal Henderson, <cal@iamcal.com> SEE ALSO
XML::Parser::Lite. perl v5.12.3 2011-06-04 XML::Parser::Lite::Tree(3pm)

Check Out this Related Man Page

LibXML(3pm)						User Contributed Perl Documentation					       LibXML(3pm)

NAME
XML::SimpleObject::LibXML - Perl extension allowing a simple(r) object representation of an XML::LibXML DOM object. SYNOPSIS
use XML::SimpleObject::LibXML; # Construct with the key/value pairs as argument; this will create its # own XML::LibXML object. my $xmlobj = new XML::SimpleObject(XML => $XML); # ... or construct with the parsed tree as the only argument, having to # create the XML::Parser object separately. my $parser = new XML::LibXML; my $dom = $parser->parse_file($file); # or $parser->parse_string($xml); my $xmlobj = new XML::SimpleObject::LibXML ($dom); my $filesobj = $xmlobj->child("files")->child("file"); $filesobj->name; $filesobj->value; $filesobj->attribute("type"); %attributes = $filesobj->attributes; @children = $filesobj->children; @some_children = $filesobj->children("some"); @children_names = $filesobj->children_names; DESCRIPTION
This is a short and simple class allowing simple object access to a parsed XML::LibXML tree, with methods for fetching children and attributes in as clean a manner as possible. My apologies for further polluting the XML:: space; this is a small and quick module, with easy and compact usage. Some will rightfully question placing another interface over the DOM methods provided by XML::LibXML, but my experience is that people appreciate the total simplicity provided by this module, despite its limitations. USAGE
$xmlobj = new XML::SimpleObject::LibXML($parser->parse_string($XML)) $parser is an XML::LibXML object. After creating $xmlobj, this object can now be used to browse the XML tree with the following methods. $xmlobj->child('NAME') This will return a new XML::SimpleObject::LibXML object using the child element NAME. $xmlobj->children('NAME') Called with an argument NAME, children() will return an array of XML::SimpleObject::LibXML objects of element NAME. Thus, if $xmlobj represents the top-level XML element, 'children' will return an array of all elements directly below the top-level that have the element name NAME. $xmlobj->children Called without arguments, 'children()' will return an array of XML::SimpleObjects::LibXML objects for all children elements of $xmlobj. Unlike XML::SimpleObject, XML::SimpleObject::LibXML retains the order of these children. $xmlobj->children_names This will return an array of all the names of child elements for $xmlobj. You can use this to step through all the children of a given element (see EXAMPLES), although multiple elements of the same name will not be identified. Use 'children()' instead. $xmlobj->value If the element represented by $xmlobj contains any PCDATA, this method will return that text data. $xmlobj->attribute('NAME') This returns the text for an attribute NAME of the XML element represented by $xmlobj. $xmlobj->attributes This returns a hash of key/value pairs for all elements in element $xmlobj. EXAMPLES
Given this XML document: <files> <file type="symlink"> <name>/etc/dosemu.conf</name> <dest>dosemu.conf-drdos703.eval</dest> </file> <file> <name>/etc/passwd</name> <bytes>948</bytes> </file> </files> You can then interpret the tree as follows: my $parser = new XML::LibXML; my $xmlobj = new XML::SimpleObject::LibXML ($parser->parse_string($XML)); print "Files: "; foreach my $element ($xmlobj->child("files")->children("file")) { print " filename: " . $element->child("name")->value . " "; if ($element->attribute("type")) { print " type: " . $element->attribute("type") . " "; } print " bytes: " . $element->child("bytes")->value . " "; } This will output: Files: filename: /etc/dosemu.conf type: symlink bytes: 20 filename: /etc/passwd bytes: 948 You can use 'children()' without arguments to step through all children of a given element: my $filesobj = $xmlobj->child("files")->child("file"); foreach my $child ($filesobj->children) { print "child: ", $child->name, ": ", $child->value, " "; } For the tree above, this will output: child: bytes: 20 child: dest: dosemu.conf-drdos703.eval child: name: /etc/dosemu.conf Using 'children_names()', you can step through all children for a given element: my $filesobj = $xmlobj->child("files"); foreach my $childname ($filesobj->children_names) { print "$childname has children: "; print join (", ", $filesobj->child($childname)->children_names), " "; } This will print: file has children: bytes, dest, name By always using 'children()', you can step through each child object, retrieving them with 'child()'. AUTHOR
Dan Brian <dan@brians.org> SEE ALSO
perl(1), XML::SimpleObject, XML::Parser, XML::LibXML. perl v5.10.1 2010-01-03 LibXML(3pm)
Man Page