Php parsing xml

Source: Internet
Author: User
Php parses xml by using the stream method, dom method, and xpath method.


Php uses stream mode.

The mechanism is as follows:
1 Create a parser using the xml_parse_create function

$ Parse = xml_parser_create ();



2. set the callback function for the parser to read the first and last parts of the tag and the callback function for reading data.

Xml_set_element_handler ($ parser, "startElement", "endElement ");

Xml_set_character_data_handle ($ parser, "characterData ");


3 parse xml

Xml_parse ($ parse, $ data, $ iseof );

The second parameter can be an xml segment. you can use file operation functions such as fopen fread feof fclose to open and read closed files.
For example

$ Fh = fopen ('article. XML', 'r ');
While (! Feof ($ fh ))
{
$ Data = fread ($ fh, 1024 );
Xml_parse ($ parse, $ data, feof ($ fh ));
}
Fclose ($ fh );


4. release

Xml_parser_free ($ parse );



In this case, the focus is on writing three callback functions:
StartElement, endElement, and charactionData.

Parameters:

StartElement ($ parse_instance, $ element_name, $ attr );
EndElement ($ parse_instance, $ element_name );
CharacterData ($ parse_instance, $ xml_data );

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.