Miscellaneous

  Home arrow Miscellaneous arrow Page 6 - Processing XML with PHP
MISCELLANEOUS

Processing XML with PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 15
    2003-10-12

    Table of Contents:
  • Processing XML with PHP
  • Create the Parser Object
  • Configure the Parser
  • Define the Callbacks
  • Read the XML Document
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Processing XML with PHP - Conclusion


    (Page 6 of 6 )

    The contents of your callback functions are obviously dependant upon the XML document your script will process and how you want to display its information. Let's assume the following sample XML document:

    <list>
      <languages type="interpreted">
        <name>PHP</name>
        <name>Python</name>
        <name>Ruby</name>
      </languages>
      <languages type="compiled">
        <name>C</name>
        <name>Fortan</name>
        <name>Pascal</name>
      </languages>
    </list>

    We can write the functions of our framework to manipulate the contents of the sample XML file.

    <?php
    $compiled_langs 
    = array();
    $interprt_langs = array();
    $flag "";
    $count 0;

    function 
    opening_element($parser$element$attributes) {
      
    /* opening XML element callback function */

      
    global $flag;

      if (
    $element == "languages")
        
    $flag $attributes["type"];
    }

    function 
    closing_element($parser$element) {
      
    /* closing XML element callback function */

      
    global $flag;

      if (
    $element == "languages")
        
    $flag "";
    }

    function 
    character_data($parser$data) {
      
    /* callback function for character data */

      
    global $flag;

      if (
    $flag == "compiled") {
        global 
    $compiled_langs;
        
    $compiled_langs[] = $data;
      }

      if (
    $flag == "interpreted") {
        global 
    $interprt_langs;
        
    $interprt_langs[] = $data;
      }
    }

    $parser xml_parser_create();
    xml_parser_set_option($parserXML_OPTION_CASE_FOLDINGfalse);
    xml_set_element_handler($parser"opening_element""closing_element");
    xml_set_character_data_handler($parser"character_data");

    $document file("sample.xml");

    foreach (
    $document as $line) {
      
    xml_parse($parser$line);
    }

    xml_parser_free($parser);

    echo 
    "The following compiled languages were found...&lt;br /&gt;";
    foreach (
    $compiled_langs as $name) {
      
    $count++;
      echo 
    "$count. $name &lt;br /&gt;";
    }
    echo 
    "&lt;br /&gt;";

    $count 0;
    echo 
    "The following interpreted languages were found...&lt;br /&gt;";
    foreach (
    $interprt_langs as $name) {
      
    $count++;
      echo 
    "$count. $name &lt;br /&gt;";
    }
    ?>

    When run, the script produces the following output:

    The following compiled languages were found...
      1. C
      2. Fortran
      3. Pascal

    The following interpreted languages were found...
      1. PHP
      2. Ruby
      3. Python

    About the Author

    Timothy Boronczyk lives in Syracuse, NY, where he works as an E-Services Coordinator for a local credit union. He has a background in elementary education, over 5 years experience in web design and has written tutorials on web design, PHP, Ruby, XML and various other topics. His hobbies include photography and composing music.


    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.
    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap