Miscellaneous
  Home arrow Miscellaneous arrow Page 6 - Processing XML with PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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? 
MISCELLANEOUS

Processing XML with PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 11
    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

  • 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


    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.

       · Is there any way to use this to parse external files, ie. RSS feeds from another...
       · This is output what i am getting.The following compiled languages were...
       · &lt;?php$compiled_langs = array();$interprt_langs = array();$flag =...
       · The program, as presented, does not work as presented. The problem, however, is not...
     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





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