Use PHP & XML to compile a mini search engine (2)

Source: Internet
Author: User
3. a simple example of XML display with PHP. The program below will parse the XML and output it to the browser according to the tree structure, and display the total number of elements at each layer. __________________________________________________________? $ Filedemo. xml; // XML file // Function for parsing XML files functionxml 3. a simple example of displaying XML with PHP



The following program parses the XML file and outputs it to the browser according to the tree structure, and displays the total number of elements at each layer.

__________________________________________________________








$ File = "demo. xml"; // XML file







// Functions used to parse XML files

Function xml_parse_from_file ($ parser, $ file)

{

If (! File_exists ($ file ))

{

Die ("Can't find file" $ file ".");

}



If (! ($ Fp = @ fopen ($ file, "r ")))

{

Die ("Can't open file" $ file ".");

}



While ($ data = fread ($ fp, 4096 ))

{

If (! Xml_parse ($ parser, $ data, feof ($ fp )))

{

Return (false );

}

}



Fclose ($ fp );



Return (true );

}







Function start_element ($ parser, $ name, $ attrs)

// If you encounter an on-element mark, execute this section,

// $ Name = a, $ attrs is an attribute array

{

Global $ level, $ levelcount, $ maxlevel;



$ Level + = 1;

If ($ level> $ maxlevel) $ maxlevel = $ level;

$ Levelcount [$ level] + = 1;

Echo"

";

For ($ I = 1; $ I <= ($ level-1); $ I ++) echo "| ";

If ($ level> 0) echo "+ ----";



Echo "". trim ($ name )."";



While (list ($ key, $ val) = each ($ attrs) {// display attributes

Echo "$ key => $ val ;";

}



}



Function stop_element ($ parser, $ name)

// An on-element mark is encountered, as shown in figureExecute this section,

{

Global $ level;



$ Level-= 1;

}



Function data ($ parser, $ data)

{

Global $ level;

If (trim ($ data )! = ""){

Echo trim ($ data );

}

}



Function showcount () // display the total number of elements in each layer

{

Global $ maxlevel, $ levelcount;

Echo"

";

For ($ I = 0; $ I <= $ maxlevel; $ I ++ ){

Echo $ levelcount [$ I]. "";

}

}



Global $ level, $ levelcount, $ maxlevel;

$ Level =-1;



// The instance that generates the parser

$ Parser = xml_parser_create ();



// Set the processing function

Xml_set_element_handler ($ parser, "start_element", "stop_element ");

Xml_set_character_data_handler ($ parser, "data ");

Xml_parser_set_option ($ parser, XML_OPTION_CASE_FOLDING, 0 );



// Parse the file

$ Ret = xml_parse_from_file ($ parser, $ file );

If (! $ Ret)

{

Die (sprintf ("XML error: % s at line % d ",

Xml_error_string (xml_get_error_code ($ parser )),

Xml_get_current_line_number ($ parser )));

}



// Release the parser

Xml_parser_free ($ parser );





Showcount ();



?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.