Home arrow Perl Programming arrow Page 3 - Using Perl With XML (part 2)

Parents And Their Children - Perl

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.

TABLE OF CONTENTS:
  1. Using Perl With XML (part 2)
  2. Meet Joe Cool
  3. Parents And Their Children
  4. What's In A Name?
  5. Welcome To The Human Race
  6. Building A Library
  7. Anyone For Chicken?
  8. Conclusions...
  9. ...And Links
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 9
February 01, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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.

 
 
>>> More Perl Programming Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: