PHP operations on XML files-read and display

Source: Internet
Author: User
In this example, the PHP file reads and displays the XML file content.

The content of the PHP file is as follows. Put the PHP file and Slashdot. XML in the same folder.

<? PHP
$ Open_tags = array (
'Story' => '<story> ',
'Title' => '<title> ',
'Url' => '<URL> ',
'Author' => '<author>'
);
$ Close_tags = array (
'Story' => '</story> ',
'Title' => '</title> ',
'Url' => '</URL> ',
'Author' => '</author>'
);
?>

<? PHP
// The following is the definition function to extract data:

// Processing start tag attributes refer
// $ Attrs is a multi-dimensional array. The key value is the attribute name and the value is the value of this attribute.
Function startelement ($ parser, $ name, $ attrs = ''){
Global $ open_tags, $ temp, $ current_tag;
$ Current_tag = $ name;
If ($ format = $ open_tags [$ name]) {
Switch ($ name ){
Case 'store ':
Echo 'new story :';
Break;
Default:
Break;
}
}
}

// $ Current_tag indicates the tag being processed, which will be used in the characterdata function.
//
// When you encounter a </story> flag, we know that you want to flush all the temporary variables to prepare for the next flag.
<Lt;/story> ',
'Title' => '</title> ',
'Url' => '</URL> ',
'Author' => '</author>'
);
?>

<? PHP
// The following is the definition function to extract data:

// Processing start tag attributes refer
// $ Attrs is a multi-dimensional array. The key value is the attribute name and the value is the value of this attribute.
Function startelement ($ parser, $ name, $ attrs = ''){
Global $ open_tags, $ temp, $ current_tag;
$ Current_tag = $ name;
If ($ format = $ open_tags [$ name]) {
Switch ($ name ){
Case 'store ':
Echo 'new story :';
Break;
Default:
Break;
}
}
}

// $ Current_tag indicates the tag being processed, which will be used in the characterdata function.
//
// When you encounter a </story> flag, we know that you want to flush all the temporary variables to prepare for the next flag.
Function endelement ($ parser, $ name, $ attrs = ''){
Global $ close_tags, $ temp, $ current_tag;
If ($ format = $ close_tags [$ name]) {
Switch ($ name ){
Case 'store ':
Return_page ($ temp );
$ Temp = '';
Break;
Default:
Break;
}
}
}

// Transmits data between elements to this function.
// For example, For <title> title here </title>, $ data is equal to 'title here'
Function characterdata ($ parser, $ data ){
Global $ current_tag, $ temp, $ catid;
Switch ($ current_tag ){
Case 'title ':
$ Temp ['title'] = $ data;
$ Current_tag = '';
Break;
Case 'url ':
$ Temp ['url'] = $ data;
$ Current_tag = '';
Break;
Case 'author ':
$ Temp ['author'] = $ data;
$ Current_tag = '';
Default:
Break;
}
}
?>

<? PHP

Function return_page (){
Global $ temp;
Echo 'o <a href = "'. $ temp ['url']. '"> '. $ temp ['title']. '</a> <br> ';
Echo 'author: '. $ temp ['author'].' <br> ';
Echo '-----------------------------';
Echo '<br> ';
}

// Analysis content
$ Xml_file = 'slashdot. xml ';

// Define the character set, which is the UTF-8 by default
$ Type = 'utf-8 ';

// Create a parser
$ Xml_parser = xml_parser_create ($ type );

// Set resolution options
Xml_parser_set_option ($ xml_parser, xml_option_case_folding, true );
Xml_parser_set_option ($ xml_parser, xml_option_target_encoding, 'utf-8 ');

// Tell PHP what function to call when discovering an element
// These functions also process element attributes.
Xml_set_element_handler ($ xml_parser, 'startelement ', 'enabledement ');

// Tell PHP what function is called for character data
Xml_set_character_data_handler ($ xml_parser, 'characterdata ');

If (! ($ Fp = fopen ($ xml_file, 'R '))){
Die ("unable to open the $ xml_file file for parsing! N ");
}

// Parse the entire file through Loops
While ($ DATA = fread ($ FP, 4096 )){
If (! ($ DATA = utf8_encode ($ data ))){
Echo 'error'. "N ";
}
If (! Xml_parse ($ xml_parser, $ data, feof ($ FP ))){
Die (sprintf ("XML error: % s at line % dnn ",
Xml_error_string (xml_get_error_code ($ xml_parser )),
Xml_get_current_line_number ($ xml_parser )));
}
}

Xml_parser_free ($ xml_parser );

?>

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.