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;
After creating $xmlobj, this object can now be used to browse the XML tree with the following methods.
<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: \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
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, "\n";
}
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), "\n";
}
This will print:
file has children: bytes, dest, name
By always using 'children()', you can step through each child object, retrieving them with 'child()'.