This example shows the structure of the starting element in a document in indent format.
Show XML element structure
<? Php
$ File = "data. xml ";
$ Depth = array ();
Function startElement ($ parser, $ name, $ attrs ){
Global $ depth;
For ($ I = 0; $ I <$ depth [$ parser]; $ I ){
Print "";
}
Print "$ name ";
$ 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 ("cocould 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 );
?>