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 );