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

Welcome To The Human Race - 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


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:

#!/usr/bin/perl # create an XML-compliant string $xml = "<?xml version=\"1.0\"?><me species=\"human\"><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 (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" print $species->getValue(); # end
Getting to an attribute value is a little more complicated than getting to an element. But hey - no gain without pain, right?

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