Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


SAX Example Code Page 31

March 8, 2002


Once we decide how each element should be displayed, it becomes easy to decide what HTML should be put in the different case statements. In this example we're using the closing </Travelpackage> tag, as the 'seperator tag' and displaying a horizontal line to the browser. If there isn't a need to associate HTML with a tag, leave the case empty.

The function characterData() is used to process the data between the start and end tags. In the travel.xml document, most elements contain data. For example, <City>Cayo Coco</City>, where Cayo Coco is the data. Now we add HTML to the data $data:

      # process data between tags

      function characterData($parser, $data) 

      {

          global $currentTag;

          # add HTML tags to the values

          switch ($currentTag) {

          case "Country_name":

          echo("<a href=\"#\">$data</a>\n");

              break;

          default:

              echo($data);

              break;

          }

      }

Now comes the code for the parser. First we create the parser, then we look at the options associated with the parser. Here we use the XML_OPTION_CASE_FOLDING option, which should always be set to false.

PHP provides another option that tells the parser what character set to use when parsing the XML file. The default value is ISO- 8859-1. We will explain this further in Chapter 23. For further information refer to the W3C's web site: http://www.w3.org/International/O-charset.html.

If the parser encounters a character that is does not recognize, (for example, a character that is outside of the ISO-8895-1 character set), then it replaces that character with a question mark (?):

      # initialize parser

    
    
      $xmlParser = xml_parser_create();

 

      $caseFold =    xml_parser_get_option($xmlParser, 

       XML_OPTION_CASE_FOLDING);

      $targetEncoding = xml_parser_get_option($xmlParser, 

       XML_OPTION_TARGET_ENCODING);

 

      if ($debug > 0) {

        echo("Debug is set to: $debug<br>\n");

        echo("Case folding is set to:
                  $caseFold<br>\n");

        echo("Target Encoding is set to: 
              $targetEncoding<br>\n");

      }

      # disable case folding

      if ($caseFold == 1) {

          xml_parser_set_option($xmlParser, 
          XML_OPTION_CASE_FOLDING, false);

      }

The preceding code determines whether case folding is set to true or false and if true, sets it to false.

Next we set the handlers or name the functions that are used. We can name these things anything as long as we're consistent throughout the code:

      # set callback functions

      xml_set_element_handler(
      $xmlParser, "startElement", 
      "endElement");

      xml_set_character_data_handler
      ($xmlParser, "characterData");

SAX Example Code Page 30
Professional PHP4 Programming
SAX Example Code Page 32


Up to => Home / Authoring / Languages / PHP / Pro




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers