SunQuest
 
       Perl
  Home arrow Perl arrow Page 4 - Using Perl With XML (part 2)
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Using Perl With XML (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2002-02-01

    Table of Contents:
  • Using Perl With XML (part 2)
  • Meet Joe Cool
  • Parents And Their Children
  • What's In A Name?
  • Welcome To The Human Race
  • Building A Library
  • Anyone For Chicken?
  • Conclusions...
  • ...And Links

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Using Perl With XML (part 2) - What's In A Name?


    (Page 4 of 9 )

    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 Articles
    More By icarus, (c) Melonfire


     

       

    PERL ARTICLES

    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway