NAME
XML::SimpleObject - Perl extension allowing a simple object representation of a parsed XML::Parser tree.
SYNOPSIS
use XML::SimpleObject;
DESCRIPTION
This is a short and simple class allowing simple object access to a parsed XML::Parser tree, with methods for fetching children and attributes in as clean a manner as possible.
USAGE
- $xmlobj = new XML::SimpleObject($parser->parse($XML))
-
$parser is an XML::Parser object created with Style "Tree":
my $parser = new XML::Parser (ErrorContext => 2, Style => "Tree");
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 object using the child element NAME.
- $xmlobj->children("NAME")
-
This will return an array of XML::SimpleObject 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->value
-
If the element represented by $xmlobj contains any CDATA, 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 parsed 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::Parser (ErrorContext => 2, Style => "Tree");
my $xmlobj = new XML::SimpleObject ($parser->parse($XML));
print "Files: \n";
foreach my $element ($xmlobj->child("files")->children("file"))
{
print " filename: " . $element->child("name")->value . "\n";
if ($element->attribute("type"))
{
print " type: " . $element->attribute("type") . "\n";
}
print " bytes: " . $element->child("bytes")->value . "\n";
}
This will output:
Files:
filename: /etc/dosemu.conf
type: symlink
bytes: 20
filename: /etc/passwd
bytes: 948
AUTHOR
Dan Brian <dbrian@brians.org>
SEE ALSO
perl(1), XML::Parser.
2 POD Errors
The following errors were encountered while parsing the POD:
- Around line 120:
'=item' outside of any '=over'
- Around line 154:
You forgot a '=back' before '=head1'