Phpxml processing, using xml_parser_create to parse, read, and generate

Source: Internet
Author: User
Tags xml parser
Php has an xml_parser_create () function for processing xml documents. let's take a look at this function. xml_parser_create () function creates an XML parser, which creates a new XML parser... php has an xml_parser_create () function for processing xml documents. let's take a look at this function.

The xml_parser_create () function creates an XML parser. this function creates a new XML parser and returns a resource handle that can be used by other XML functions.

Syntax: xml_parser_create (encoding)

Parameter description

Optional. specifies the output encoding.

The instance code is as follows:

 some character data";$parser = xml_parser_create();xml_set_character_data_handler($parser, 'cdata_handler');if (xml_parse($parser, $xml, true)) print ("Your XML document is well-formed.");else print ("Your XML document is not well-formed.");xml_parser_free($parser);?>

Example 2: The code is as follows:

 bar
 Parse error in %s (%s)", htmlspecialchars($xml) , xml_error_string(xml_get_error_code($p))));}xml_parser_free($p);?>

Example 3 code:

 ";$p = xml_parser_create();xml_set_processing_instruction_handler($p, 'pi_handler');if (!xml_parse($p, $xml, true)) die(sprintf("Parse error in %s (%s)", htmlspecialchars($xml) , xml_error_string(xml_get_error_code($p))));else print ("XML processing complete.n");xml_parser_free($p);?>

Note:The optional parameter encoding is used in PHP 4 to specify the encoding method of the XML input to be parsed.

PHP 5 starts and automatically detects the input XML encoding. Therefore, the encoding parameter is only used to specify the encoding of the parsed output data.

In PHP 4, the default output encoding is the same as the input data encoding. if an empty string is passed, the parser will try to search for the first 3 or 4 bytes to determine the document encoding.

In PHP 5.0.0 and 5.0.1, the default output character encoding is ISO-8859-1, while PHP 5.0.2 and later versions are UTF-8, the parser supports the encoding of ISO-8859-1, UTF-8 and US-ASCII.


Article link:

Save this article for favorites!

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.