The model uses the indent pattern to show the structure of the starting element in a document. Show XML element structure? Php $ file = "data. xml "; $ depth = array (); functionstartElement ($ parser, $ name, $ attrs) {global $ depth; for ($ I = 0; $ I $ d
The model uses the indent pattern to show the structure of the starting element in a document.
Show XML element structure
$ File = 'data. XML ';
$ Depth = array ();
Function startElement ($ parser, $ name, $ attrs ){
Global $ depth;
For ($ I = 0; $ I <$ depth [$ parser]; $ I ){
Print '';
}
Print '$ name \ n ';
$ Depth [$ parser];
}
Function endElement ($ parser, $ name ){
Global $ depth;
$ Depth [$ parser] --;
}
$ Xml_parser = xml_parser_create ();
Xml_set_element_handler ($ xml_parser, 'startelement ', 'enabledement ');
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 );
?>