SYNOPSIS

  use XML::SimpleObject;

  # Construct with the key/value pairs as argument; this will create its
  # own XML::Parser object.
  my $xmlobj = new XML::SimpleObject(XML => $XML, ErrorContext => 2);

  # ... or construct with the parsed tree as the only argument, having to
  # create the XML::Parser object separately.
  my $parser = new XML::Parser (ErrorContext => 2, Style => "Tree");
  my $xmlobj = new XML::SimpleObject ($parser->parse($XML));

  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");
  @chilren_names = $filesobj->children_names;

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. My apologies for further polluting the \s-1XML::\s0 space; this is a small and quick module, with easy and compact usage. See XML::SimpleObject::LibXML for the same interface for XML::LibXML.

USAGE

$parser is an XML::Parser object created with Style \*(L"Tree\*(R": my $parser = new XML::Parser (ErrorContext => 2, Style => "Tree"); After creating $xmlobj, this object can now be used to browse the \s-1XML\s0 tree with the following methods. This will return a new XML::SimpleObject object using the child element \s-1NAME\s0. Called with an argument \s-1NAME\s0, children() will return an array of XML::SimpleObject objects of element \s-1NAME\s0. Thus, if $xmlobj represents the top-level \s-1XML\s0 element, 'children' will return an array of all elements directly below the top-level that have the element name \s-1NAME\s0. Called without arguments, 'children()' will return an array of XML::SimpleObject s for all children elements of $xmlobj. These are not in the order they occur in the \s-1XML\s0 document. 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 \s-1EXAMPLES\s0). Each name will occur only once, even if multiple children exist with that name. If the element represented by $xmlobj contains any \s-1PCDATA\s0, this method will return that text data. This returns the text for an attribute \s-1NAME\s0 of the \s-1XML\s0 element represented by $xmlobj. This returns a hash of key/value pairs for all elements in element $xmlobj.

EXAMPLES

Given this \s-1XML\s0 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

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()'.

AUTHOR

Dan Brian <[email protected]>

RELATED TO XML::SimpleObject…

perl\|(1), XML::Parser.