SimpleObject(3pm)

πŸ“¦
version 0.53 Β· 2 versions
  • 0.53 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.53 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ SimpleObject(3pm) ✨

okay bestie, welcome to SimpleObject β€” a User Contributed Perl Documentation page, no cap. It's short, it's sweet, and it's literally just vibes for your XML trees. Let's get into it πŸ’–


🌸 NAME 🌸

XML::SimpleObject β€” a Perl extension that gives you a simple object representation of a parsed XML::Parser tree. it's giving "I don't want to think about trees, I want objects" energy πŸ¦‹


πŸŽ€ 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;

(yes, @chilren_names is spelled like that in the original and we respect it, don't touch it πŸ’…)


πŸ’œ DESCRIPTION πŸ’œ

so this is a short and simple class that lets you get at a parsed XML::Parser tree with simple object access, with methods for fetching children and attributes in as clean a manner as possible. the author literally apologizes for further polluting the XML:: space β€” this is a small and quick module, with easy and compact usage. icon behavior honestly 😌

psst: if you want the same interface but for XML::LibXML, go look at XML::SimpleObject::LibXML πŸ‘‘

🫢 USAGE 🫢

$xmlobj = new XML::SimpleObject($parser->parse($XML))

here $parser is an XML::Parser object that you made with Style "Tree":

my $parser = new XML::Parser (ErrorContext => 2, Style => "Tree");

once $xmlobj exists, you can use it to browse the XML tree with all the methods below, bestie. let's go 🌈

$xmlobj->child('NAME')

this returns a new XML::SimpleObject object built from the child element NAME. one baby object at a time, slay πŸ’…

$xmlobj->children('NAME')

call this with an argument NAME and children() hands you back an array of XML::SimpleObject objects of element NAME. so if $xmlobj represents the top-level XML element, children returns an array of every element directly below the top-level that has the element name NAME. periodt ✨

$xmlobj->children

called with no arguments, children() returns an array of XML::SimpleObjects for all children elements of $xmlobj. heads up though:

⚠️ these are not in the order they occur in the XML document. the order is a mystery, a little chaos goblin, we don't question it πŸ’€

$xmlobj->children_names

this returns an array of all the names of child elements for $xmlobj. you can use it to step through all the children of a given element (see EXAMPLES below 🫢). each name will occur only once, even if multiple children exist with that name. very efficient, very demure 🌸

$xmlobj->value

if the element represented by $xmlobj contains any PCDATA, this method returns that text data. it's giving the actual words πŸ“

$xmlobj->attribute('NAME')

this returns the text for an attribute NAME of the XML element represented by $xmlobj. tiny, useful, iconic πŸ‘‘

$xmlobj->attributes

this returns a hash of key/value pairs for all elements in element $xmlobj. the whole squad, in hash form πŸ’œ


🌈 EXAMPLES 🌈

okay so picture 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 like this:

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";
}

and 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

and by always using children(), you can step through each child object, retrieving them with child(). chain them together, bestie, that's the whole vibe πŸ¦‹


πŸ‘‘ AUTHOR πŸ‘‘

Dan Brian <dbrian@brians.org>


⭐ SEE ALSO ⭐


okay that's the entire page, we ate and left no crumbs πŸ’… this was SimpleObject(3pm), and the footer vibes were perl v5.34.0 dated 2022-06-28. bye bestie, go parse something pretty πŸŽ€πŸ’–πŸ¦‹

πŸ“¦ where this page comes from