downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  

<Character EncodingXML Tag Mapping Example>
Last updated: Thu, 26 Jun 2008

Examples

Table of Contents

XML Element Structure Example

This first example displays the structure of the start elements in a document with indentation.

Example #1 Show XML Element Structure

<?php
$file
= "data.xml";
$depth = array();

function
startElement($parser, $name, $attrs)
{
   global
$depth;
   for (
$i = 0; $i < $depth[$parser]; $i++) {
       echo
"  ";
   }
   echo
"$name\n";
  
$depth[$parser]++;
}

function
endElement($parser, $name)
{
   global
$depth;
  
$depth[$parser]--;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!(
$fp = fopen($file, "r"))) {
   die(
"could not open XML input");
}

while (
$data = fread($fp, 4096)) {
   if (!
xml_parse($xml_parser, $data, feof($fp))) {
       die(
sprintf("XML error: %s at line %d",
                  
xml_error_string(xml_get_error_code($xml_parser)),
                  
xml_get_current_line_number($xml_parser)));
   }
}
xml_parser_free($xml_parser);
?>



add a noteadd a note User Contributed Notes
Examples
There are no user contributed notes for this page.




<Character EncodingXML Tag Mapping Example>
Last updated: Thu, 26 Jun 2008
show source | credits | sitemap | contact | advertising | mirror sites
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: http://phpbuilder.com/
Last updated: Tue Nov 1 20:20:59 2005 EST
Columns / Articles | Tips / Quickies | News | News Linking and RSS Feeds | Shared Code Library
Mail Archives | Support / Discussion Forums | Get Started! Links | Contribute! | Docs