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

What's In A Name? - 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.

  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


Once you've obtained a reference to a node, a number of other methods become available to help you obtain the name and value of that node, as well as references to parent and child nodes. Take a look:

#!/usr/bin/perl # create an XML-compliant string $xml = "<?xml version=\"1.0\"?><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 root node $root = $doc->getDocumentElement(); # get name of root node # returns "me" print $root->getNodeName(); # get children as array @children = $root->getChildNodes(); # this is the "name" element under "me" # I could also have used $root->getFirstChild() to get here $firstChild = $children[0]; # returns "name" print $firstChild->getNodeName(); # returns "1" print $firstChild->getNodeType(); # now to access the value of the text node under "name" $text = $firstChild->getFirstChild(); # returns "Joe Cool" print $text->getData(); # returns "#text" print $text->getNodeName(); # returns "3" print $text->getNodeType(); # go back up the tree # start from the "name" element and get its parent $parent = $firstChild->getParentNode(); # check the name - it should be "me" # yes it is! print $parent->getNodeName(); # end
As you can see, the getNodeName() and getNodeType() methods provide access to basic information about the node currently under examination. The children of this node can be obtained with the getChildNodes() method previously discussed, and node parents can be obtained with the getParentNode() method. It's fairly simple, and - once you play with it a little - you'll get the hang of how it works.

A quick note on the getNodeType() method above: every node is of a specific type, and this property returns a numeric code corresponding to the type. A complete list of defined types is available in the Perl documentation for the XML::DOM package.

Note also that the text within an element's opening and closing tags is treated as a child node of the corresponding element node, and is returned as an object. This object comes with a getData() method, which returns the actual content nested within the element's opening and closing tags. You'll see this again in a few pages.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: