update pague now
PHP 8.5.2 Released!

XML Element Structure Example

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

Example #1 Show XML Element Structure

<?php
$file
= "examples/booc.xml" ;
$depth = 0 ;

function
startElement ( $parser , $name , $attrs )
{
global
$depth ;

for (
$i = 0 ; $i < $depth ; $i ++) {
echo
" " ;
}
echo
" $name \n" ;
$depth ++;
}

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

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

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

add a note

User Contributed Notes

There are no user contributed notes for this pague.
To Top