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 32

March 8, 2002

Finally, we open and parse the XML file:

      # open XML file

      if (!($fp = fopen($file, "r"))) {

          die("Cannot open XML data file: $file");

      }

 

      # read and parse data

      while ($data = fread($fp, 4096)) {

The error handling section checks to see if the XML parser is not at the end of the XML file when an error occurs. If there is an error and we're not at the end of the file, then we capture and send the error code and the line where the error occurred to the browser:

          # error handling

          if (!xml_parse($xmlParser, $data, feof($fp))) {

              die(sprintf("XML error: %s at line %d",

                   xml_error_string(xml_get_error_code($xmlParser)),

                   xml_get_current_line_number($xmlParser)));

              xml_parser_free($xmlParser);

          }

      }

      # free up the parser

      xml_parser_free($xmlParser);

      ?>

    </table>

  </body>

</html>

There are a total of five error messages that can be captured. For more information, refer to: http://www.php.net/manual/en/function.xml-parse.php.

xml_parse() has three parameters – the name of the parser ($xmlParser), the data to check ($data), and an optional parameter to indicate that the last piece of data is being sent in this chuck of data. If this optional parameter is set and true then this is the last piece of data. If it is set and false then this isn't the last piece of data.

The feof() function checks to see if the pointer is at the end of the file or if there is an error. If it is at the end of the file or there is an error, then it returns true. If it isn't or there's no error, then it returns false. It is a good practice to validate the generated HTML and make sure there are no errors. Performing this validation is a good way to debug the code if you're having problems.

This figure shows the output of the above code:

SAX Example Code Page 31
Professional PHP4 Programming


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