XML file operations in PHP: Reading and displaying

Source: Internet
Author: User
Tags flush php file prepare

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 we encounter the </STORY> Mark, we know that we need to flush all the temporary variables to prepare for the next mark.
<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 we encounter the </STORY> Mark, we know that we need to flush all the temporary variables to prepare for the next mark.
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;

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.