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.
The Document object comes with another useful method, one which enables you to gain access to information about the document's XML version and character encoding. It's called the getXMLDecl() method, and it returns yet another object, this one representing the standard XML declaration that appears at the top of every XML document. Take a look:
#!/usr/bin/perl
# create an XML-compliant string
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><me><name>Joe
Cool</name><age>24</age><sex>male</sex></me>";
# include package
use XML::DOM;
# instantiate parser
$xp = new XML::DOM::Parser();
# parse and create tree
$doc = $xp->parse($xml);
# get XML PI
$decl = $doc->getXMLDecl();
# get XML version
print $decl->getVersion();
# get encoding
print $decl->getEncoding();
# get whether standalone
print $decl->getStandalone();
# end
As you can see, the newly-created XMLDecl object comes with a bunch of object methods of its own. These methods provide a simple way to access the document's XML version, character encoding and status.
Using the Document object, it's also possible to obtain references to other nodes in the XML tree, and manipulate them using standard methods. Since the entire document is represented as a tree, the first step is always to obtain a reference to the tree root, or the outermost document element, and use this a stepping stone to other, deeper branches. Consider the following example, which demonstrates how to do this:
#!/usr/bin/perl
# create an XML-compliant string$xml = "<?xml version=\"1.0\"?><me><name>JoeCool</name><age>24</age><sex>male</sex></me>";# include packageuse XML::DOM;# instantiate parser$xp = new XML::DOM::Parser();# parse and create tree$doc = $xp->parse($xml);# get root node "me"$root = $doc->getDocumentElement();# end
An option here would be to use the getChildNodes() method, which is a common method available to every single node in the document tree. The following code snippet is identical to the one above:
#!/usr/bin/perl
# create an XML-compliant string$xml = "<?xml version=\"1.0\"?><me><name>JoeCool</name><age>24</age><sex>male</sex></me>";# include packageuse XML::DOM;# instantiate parser$xp = new XML::DOM::Parser();# parse and create tree$doc = $xp->parse($xml);# get root node "me"@children = $doc->getChildNodes();$root = $children[0];# end
Note that the getChildNodes() method returns an array of nodes under the current node; each of these nodes is again an object instance of the Node class, and comes with methods to access the node name, type and content. Let's look at that next.