Perl
  Home arrow Perl arrow Page 3 - 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 
Moblin 
JMSL Numerical Library 
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


    Using Perl With XML (part 2) - Parents And Their Children


    (Page 3 of 9 )

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


     

       

    PERL ARTICLES

    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl
    - 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





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