Content Management Code
  Home arrow Content Management Code arrow XML Reader
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? 
CONTENT MANAGEMENT CODE

XML Reader
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2003-06-20

    Table of Contents:

    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


    Light XML Reader, you'll gonna need it to read XML content.

    By : hermawan

    <?php
    /********************************************************
    * Class Name : XML-Reader *
    * Publish On : June 20th, 2003 *
    * Producer : Hermawan Haryanto (http://hermawan.com) *
    * Version : 1.0 *
    * License : GPL (General Public License) *
    * *
    * Credits: *
    * 1. Hans Anderson (me@hansanderson.com) *
    * http://www.hansanderson.com *
    * 2. John Doe (acebone@f2s.com) *
    ********************************************************/

    class xmlreader {
    var $_data;
    var $_white;
    var $_xml_url;
    function xmlreader ($xml_url = "")
    {
    $this->_white = 1;
    if (trim($xml_url) != "") $this->set_xml_url ($xml_url);
    }
    function set_xml_url ($url)
    {
    $this->_xml_url = $url;
    }
    function read ()
    {
    if (!$this->_xml_url) $this->error ("XML File is not assigned.");
    $fp = fopen ($this->_xml_url, "r");
    while (!feof ($fp)) $this->_data .= fgets($fp, 4096);
    fclose ($fp);
    $this->_data = trim ($this->_data);
    }
    function parse ()
    {
    $this->read();
    if (trim ($this->_data) == "") $this->error ("Data not ready.");
    $vals = $index = $array = array();
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $this->_white);
    xml_parse_into_struct($parser, $this->_data, $vals, $index);
    xml_parser_free($parser);
    $i = 0;
    $tagname = $vals[$i]['tag'];
    if ( isset ($vals[$i]['attributes'] ) )
    {
    $array[$tagname]['@'] = $vals[$i]['attributes'];
    }
    else
    {
    $array[$tagname]['@'] = array();
    }
    $array[$tagname]["#"] = $this->xml_depth($vals, $i);
    return $array;
    }
    function xml_depth($vals, &$i) {
    $children = array();
    if ( isset($vals[$i]['value']) )
    {
    array_push($children, $vals[$i]['value']);
    }
    while (++$i < count($vals)) {
    switch ($vals[$i]['type']) {
    case 'open':
    if ( isset ( $vals[$i]['tag'] ) )
    {
    $tagname = $vals[$i]['tag'];
    }
    else
    {
    $tagname = '';
    }
    if ( isset ( $children[$tagname] ) )
    {
    $size = sizeof($children[$tagname]);
    }
    else
    {
    $size = 0;
    }
    if ( isset ( $vals[$i]['attributes'] ) )
    {
    $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
    }
    $children[$tagname][$size]['#'] = $this->xml_depth($vals, $i);
    break;
    case 'cdata':
    array_push($children, $vals[$i]['value']);
    break;
    case 'complete':
    $tagname = $vals[$i]['tag'];
    if( isset ($children[$tagname]) )
    {
    $size = sizeof($children[$tagname]);
    }
    else
    {
    $size = 0;
    }
    if( isset ( $vals[$i]['value'] ) )
    {
    $children[$tagname][$size]["#"] = $vals[$i]['value'];
    }
    else
    {
    $children[$tagname][$size]["#"] = '';
    }
    if ( isset ($vals[$i]['attributes']) )
    {
    $children[$tagname][$size]['@'] = $vals[$i]['attributes'];
    }
    break;
    case 'close':
    return $children;
    break;
    }
    }
    return $children;
    }
    function traverse_xmlize($array, $arrName = "array", $level = 0) {
    foreach($array as $key=>$val)
    {
    if ( is_array($val) )
    {
    traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1);
    }
    else
    {
    $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";
    }
    }
    return 1;
    }
    function error ($str)
    {
    print get_class ($this)." ".$this->version()." => $str";
    exit();
    }
    function version ()
    {
    return "1.0";
    }
    };
    // EXAMPLE
    $xmlurl = "http://hermawan.com/kurs/bca.xml";
    $xmlreader = new xmlreader ($xmlurl);
    $xml = $xmlreader->parse();
    $money = $xml["KURS"]["#"]["MONEY"];
    print "<pre>";
    print "<b>Money \tBuy \tSale</b>\n";
    for ($i=0; $i<sizeof ($money); $i++)
    {
    $matauang = $money[$i];
    $name = $matauang["@"]["NAME"];
    $buy = $matauang["#"]["BUY"][0]["#"];
    $sale = $matauang["#"]["SALE"][0]["#"];
    print "$name \t$buy \t$sale\n";
    }
    ?>
    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.

    More Content Management Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Driving Business Success with Rational Process Library

    Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance.
    FREE! Go There Now!


    NEW! Calling all CC Power Users – and those that would like to be!

    Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development
    FREE! Go There Now!


    NEW! Evaluate Rational Business Developer V7.1

    Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities.
    FREE! Go There Now!


    NEW! Info 2.0: Harnessing the power of Web 2.0 and Enterprise Mashups

    Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started.
    FREE! Go There Now!


    NEW! Rational Talks to You:Per Kroll on Rational Method Composer Plug-in customization

    Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Talks to You: Manage RUP-based CMMI initiatives

    Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Testing eKits

    Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing.
    FREE! Go There Now!


    NEW! Trial download: IBM Informix Dynamic Server Express Edition V11.0

    Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Functional Tester V7.0.1

    Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications.
    FREE! Go There Now!


    Refresh! IBM Rational Systems Development Solution eKit

    With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    CONTENT MANAGEMENT CODE ARTICLES

    - V2 CMS - Content Management System
    - VSNS Lemon
    - Country List For Forms Using SQL
    - eggblog
    - Table generation class
    - STP Simple Template Parser
    - class Vision_To_Form_Elements
    - Cascade Drop Down
    - Cura - CMS
    - Syntax Desktop
    - 216 color table
    - Simple Mini Poll class library (SimPoll)
    - Regex Generator
    - Siteseed
    - Company WebSite Builder PRO





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