They say there's more than one way to skin a cat - and that'stwice as true when you're a Perl developer. In this concluding article onXML parsing with Perl, find out how the XML::DOM package provides analternative technique for manipulating XML elements and attributes, andcompare the two approaches to see which one works best for you.
Just as it's possible to access elements and their content, it's also possible to access element attributes and their values. The getAttributes() method of the Node object provides access to a list of all available attributes, and the getNamedItem() and getValue() methods make it possible to access specific attributes and their values. Take a look at a demonstration of how it all works:
# create an XML-compliant string
$xml = "<?xml version=\"1.0\"?><me species=\"human\"><name>Joe
# include package
# instantiate parser
$xp = new XML::DOM::Parser();
# parse and create tree
$doc = $xp->parse($xml);
# get root node (Node object)
$root = $doc->getDocumentElement();
# get attributes (NamedNodeMap object)
$attribs = $root->getAttributes();
# get specific attribute (Attr object)
$species = $attribs->getNamedItem("species");
# get value of attribute
# returns "human"
Getting to an attribute value is a little more complicated than getting to an element. But hey - no gain without pain, right?