Example of using XML parser in PHP: expat

Source: Internet
Author: User
PHP Resource

Http://www.w3schools.com/php/default.asp

<? PHP

// Initialize the XML Parser
$ Parser = xml_parser_create ();

// Function to use at the start of an element
Function start ($ parser, $ element_name, $ element_attrs)
{
Switch ($ element_name)
{
Case "NOTE ":
Echo "-- note -- <br/> ";
Break;
Case "":
Echo ":";
Break;
Case "from ":
Echo "from :";
Break;
Case "heading ":
Echo "heading :";
Break;
Case "body ":
Echo "message :";
}
}

// Function to use at the end of an element
Function stop ($ parser, $ element_name)
{
Echo "<br/> ";
}

// Function to use when finding character data
Function char ($ parser, $ data)
{
Echo $ data;
}

// Specify element Handler
Xml_set_element_handler ($ parser, "Start", "stop ");

// Specify data handler
Xml_set_character_data_handler ($ parser, "char ");

// Open XML file
$ Fp = fopen ("test. xml", "R ");

// Read data
While ($ DATA = fread ($ FP, 4096 ))
{
Xml_parse ($ parser, $ data, feof ($ FP) or
Die (sprintf ("XML error: % s at line % d ",
Xml_error_string (xml_get_error_code ($ parser )),
Xml_get_current_line_number ($ parser )));
}

// Free the XML Parser
Xml_parser_free ($ parser );

?>

Related Article

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.