PHP Operation XML File: Read, display

Source: Internet
Author: User
Tags format definition character set flush variables php file
In this case, the PHP file reads and displays the contents of the XML file

The following is the contents of PHP file, the php file and Slashdot.xml in the same folder can be

<?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 the data:

The property that handles the start tag refers to
$attrs is a multidimensional array with the key value being the property name and the value being the value of the property
function startelement ($parser, $name, $attrs = ' ){
Global $open _tags, $temp, $current _tag ;
$current _tag = $name ;
if ($format = $open _tags[$name ]){
Switch ($name ){
Case ' STORY ' :
Echo ' New story: ' ;
Break
Default
Break
}
}
}

//$current _tag tells us the tag we're working on, and we'll use it in the Characterdata function later.
//
When we encounter the </STORY> tag we know that we have to flush all the temporary variables ready to operate the next tag
<lt;/STORY> ' ,
' TITLE ' => ' </TITLE> ' ,
' URL ' => ' </URL> ' ,
' AUTHOR ' => ' </AUTHOR> '
);
?>

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

The property that handles the start tag refers to
$attrs is a multidimensional array with the key value being the property name and the value being the value of the property
function startelement ($parser, $name, $attrs = ' ){
Global $open _tags, $temp, $current _tag ;
$current _tag = $name ;
if ($format = $open _tags[$name ]){
Switch ($name ){
Case ' STORY ' :
Echo ' New story: ' ;
Break
Default
Break
}
}
}

//$current _tag tells us the tag we're working on, and we'll use it in the Characterdata function later.
//
When we encounter the </STORY> tag we know that we have to flush all the temporary variables ready to operate the next tag
function EndElement ($parser, $name, $attrs = ' ){
Global $close _tags, $temp, $current _tag ;
if ($format = $close _tags[$name ]){
Switch ($name ){
Case ' STORY ' :
Return_page ($temp );
$temp = ' ;
Break
Default
Break
}
}
}

//Sent to this function is the data between the elements
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 of content
$xml _file = ' Slashdot.xml ' ;

//define character set, default is UTF-8
$type = ' UTF-8 ' ;

//Create 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 elements
These functions also handle the attributes of the element
Xml_set_element_handler ($xml _parser, ' startelement ', ' endelement ' );

//Tell PHP what function to call the character data
Xml_set_character_data_handler ($xml _parser, ' Characterdata ' );

if (!) ( $fp = fopen ($xml _file, ' R ' ))) {
Die ("Cannot open $xml _file file for resolution!n" );
}

//loop to parse the entire file
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.