XML
  Home arrow XML arrow Page 2 - Using PHP with XML (part 2)
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
XML

Using PHP with XML (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2001-06-20

    Table of Contents:
  • Using PHP with XML (part 2)
  • Meet Joe Cool
  • 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 PHP with XML (part 2) - Meet Joe Cool
    (Page 2 of 6 )

    As before, the first order of business is to read the XML data into memory. PHP offers three functions to do this: the xmldoc() and xmltree() functions accept a string containing XML data as argument, and build a tree structure from that string, while the xmldocfile() function accepts a filename as argument, and builds a DOM tree after reading the data in the file.

    <? // create an XML-compliant string $XMLDataString = "<?xml version="1.0"?" . "><me><name>Joe Cool</name><age>24</age><sex>male</sex></me>"; // create a document object $XMLDoc = xmldoc($XMLDataString); // create a tree object $XMLTree = xmltree($XMLDataString); ?>
    You can also load a file directly.

    <? // data file $XMLFile = "me.xml"; // create a document object $XMLDoc = xmldocfile($XMLFile); ?>
    Windows users should note that xmldocfile() needs the complete path to the file, including drive letter, to work correctly.{mospagebreak title=Parents And Their Children} Once the document has been read in, a number of methods become available to traverse the document tree.

    If you're using a document object, such as the one returned by xmldoc() and xmldocfile(), you can use the following methods to obtain references to other nodes in the tree,

    <? // data file $file = "library.xml"; // create a document object $dom = xmldocfile($file); // echo these values to see the object type // get root node $dom->root(); // get children under the root node as array $dom->children(); ?>
    or to properties of the document itself.

    <? // data file $file = "library.xml"; // create a document object $dom = xmldocfile($file); // get XML version echo $dom->version; // get XML encoding echo $dom->encoding; // get whether standalone file echo $dom->standalone; // get XML URL echo $dom->url; // get XML character set echo $dom->charset; ?>
    Once you've obtained a reference to a node, a number of other methods and properties 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:

    <? // data file $str = "<?xml version="1.0"?><me><name>Joe Cool</name><age>24</age><sex>male</sex></me>"; // create a document object $dom = xmldoc($str); // get reference to root node $root = $dom->root(); // get name of node - "me" echo $root->name; // get children of node, as array $children = $root->children(); // get first child node $firstChild = $children[0]; // let's see a few node properties // get name of first child node - "name" echo $firstChild->name; // get content of first child node - "Joe Cool" echo $firstChild->content; // get type of first child node - "1", or "XML_ELEMENT_NODE" echo $firstChild->type; // go back up the tree! // get parent of child - this should be <me> $parentNode = $firstChild->parent(); // check it...yes, it is "me"! echo $parentNode->name; ?>
    A quick note on the "type" property above: every node is of a specific type, and this property returns a numeric and textual code corresponding to the type. A complete list of types is available in the PHP manual.{mospagebreak title=Welcome To The Human Race} Many of the object methods you just saw are also available as regular functions; for example,

    <? // data file $str = "<?xml version="1.0"?><me><name>Joe Cool</name><age>24</age><sex>male</sex></me>"; // create a document object $dom = xmldoc($str); // get reference to root node $root = $dom->root(); // get name of node - "me" echo $root->name; // you could also do this! // get reference to root node $root = domxml_root($dom); // get name of node - "me" echo $root->name; ?>
    Finally, element attributes may be obtained using either the $node->attributes() object method, or the dom_attributes() function.

    <? // data file $str = "<?xml version="1.0"?><me species="human"><name>Joe Cool</name><age>24</age><sex>male</sex></me>"; // create a document object $dom = xmldoc($str); // get reference to root node $root = $dom->root(); // get attribute collection // $attributes = $root->attributes(); // you could also do this! // $attributes = domxml_attributes($root); // get first attribute name - "species" echo $attributes[0]->name; // get first attribute value - "human" echo $attributes[0]->children[0]->content; // you could also do this! // echo domxml_getattr($root, "species"); ?>

    More XML Articles
    More By icarus, (c) Melonfire


     

       

    XML ARTICLES

    - How to Set Up Podcasting and Vodcasting
    - Creating an RSS Reader Application
    - Building an RSS File
    - An Introduction to XUL Part 6
    - An Introduction to XUL Part 5
    - An Introduction to XUL Part 4
    - An Introduction to XUL Part 3
    - An Introduction to XUL Part 2
    - An Introduction to XUL Part 1
    - XML Matters: Practical XML Data Design and M...
    - Practical XML Data Design and Manipulation f...
    - SimpleXML
    - XForms Basics, Part 3
    - XForms Basics, Part 2
    - XForms Basics

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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