XML
  Home arrow XML arrow Page 6 - Using PHP with XML (part 1)
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 1)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 18
    2001-06-18

    Table of Contents:
  • Using PHP with XML (part 1)
  • Getting Down To Business
  • Let's Talk About SAX
  • Breaking It Down
  • Call Me Back
  • What's For Dinner?

  • 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Using PHP with XML (part 1) - What's For Dinner?
    (Page 6 of 6 )

    Here's another, slightly more complex example using the SAX parser, and one of my favourite meals.

    <?xml version="1.0"?> <recipe> <name>Chicken Tikka</name> <author>Anonymous</author> <date>1 June 1999</date> <ingredients> <item> <desc>Boneless chicken breasts</desc> <quantity>2</quantity> </item> <item> <desc>Chopped onions</desc> <quantity>2</quantity> </item> <item> <desc>Ginger</desc> <quantity>1 tsp</quantity> </item> <item> <desc>Garlic</desc> <quantity>1 tsp</quantity> </item> <item> <desc>Red chili powder</desc> <quantity>1 tsp</quantity> </item> <item> <desc>Coriander seeds</desc> <quantity>1 tsp</quantity> </item> <item> <desc>Lime juice</desc> <quantity>2 tbsp</quantity> </item> <item> <desc>Butter</desc> <quantity>1 tbsp</quantity> </item> </ingredients> <servings> 3 </servings> <process> <step>Cut chicken into cubes, wash and apply lime juice and salt</step> <step>Add ginger, garlic, chili, coriander and lime juice in a separate bowl</step> <step>Mix well, and add chicken to marinate for 3-4 hours</step> <step>Place chicken pieces on skewers and barbeque</step> <step>Remove, apply butter, and barbeque again until meat is tender</step> <step>Garnish with lemon and chopped onions</step> </process> </recipe>
    This time, my PHP script won't be using a "switch" statement when I parse the file above; instead, I'm going to be keying tag names to values in a hash. Each of the tags in the XML file above will be replaced with appropriate HTML markup.

    <html> <head> </head> <body bgcolor="white"> <? // data file $file = "recipe.xml"; /* hash of tag names mapped to HTML markup "RECIPE" => start a new block "NAME" => in bold "INGREDIENTS" => unordered list "DESC" => list items "PROCESS" => ordered list "STEP" => list items */ $startTags = array( "RECIPE" => "<hr>", "NAME" => "<font size=+2>", "DATE" => "<i>(", "AUTHOR" => "<b>", "SERVINGS" => "<i>Serves ", "INGREDIENTS" => "<h3>Ingredients:</h3><ul>", "DESC" => "<li>", "QUANTITY" => "(", "PROCESS" => "<h3>Preparation:</h3><ol>", "STEP" => "<li>" ); // close tags opened above $endTags = array( "NAME" => "</font><br>", "DATE" => ")</i>", "AUTHOR" => "</b>", "INGREDIENTS" => "</ul>", "QUANTITY" => ")", "SERVINGS" => "</i>", "PROCESS" => "</ol>", ); function startElement($parser, $name, $attrs) { global $startTags; // if tag exists as key, print value if ($startTags[$name]) { echo $startTags[$name]; } } function endElement($parser, $name) { global $endTags; if ($endTags[$name]) { echo $endTags[$name]; } } // process data between tags function characterData($parser, $data) { echo $data; } // initialize parser $xml_parser = xml_parser_create(); // set callback functions xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); // open XML file if (!($fp = fopen($file, "r"))) { die("Cannot locate XML data file: $file"); } // read and parse data while ($data = fread($fp, 4096)) { // error handler if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } // clean up xml_parser_free($xml_parser); ?> </body> </html>
    In this case, I've set up two hashes (associative arrays), one for opening tags and one for closing tags. When the parse encounters an XML tag, it looks up the array to see if the tag exists as a key. If it does, the corresponding value (HTML markup) is printed. This method does away with the slightly cumbersome "switch" statements of the previous example, and is easier to read and understand.

    Here's the output:



    That's about it for the moment. Over the last few pages, I've discussed using PHP's built-in SAX parser to process an XML file and mark up the data within it with HTML tags. However, just as there's more than one way to skin a cat, there's more than one way to process XML data in PHP. In the second part of this article, I'll be looking at an alternative technique of parsing an XML file, this time using the DOM. Make sure you come back for that one!
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    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 2 hosted by Hostway