SimpleObject(3pm)
β¨ 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 β
perl(1)XML::Parser
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
- debian bookworm: libxml-simpleobject-perl 0.53-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libxml-simpleobject-perl 0.53-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libxml-simpleobject-perl 0.53-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libxml-simpleobject-perl 0.53-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libxml-simpleobject-perl 0.53-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms