PHP code for parsing the 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 "$ namen ";
- $ 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 );
- ?>
The above code example is a detailed analysis of PHP parsing XML element structure. I hope you can fully understand it.