XML
  Home arrow XML arrow Page 2 - Using PHP with XML (part 2)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
XML

Using PHP with XML (part 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 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

    - Flex Array Collection Sort and Filtering
    - The Flex Tree Control
    - Flex List Controls
    - Working with Flex and Datagrids
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek