In PHP, xml_set_element_handler() registers the two functions that run when the parser meets an opening tag and a closing tag.
The start handler receives the tag name and an array of its attributes. The end handler receives only the tag name. These two handlers are the core of event-based XML reading in PHP, and they are usually combined with xml_set_character_data_handler().
Syntax
Call xml_set_element_handler() with the values you need. Start with this line.
The start handler is called as start_handler(XMLParser $parser, string $name, array $attributes) and the end handler as end_handler(XMLParser $parser, string $name). Tag names and attribute names arrive in UPPERCASE unless you turn off XML_OPTION_CASE_FOLDING.
The start handler receives the tag name and an array of attributes as its second and third arguments.
Because case folding is on, the tag name is ITEM and the attribute key is ID.
Each id value is added to the ids array.
implode() joins the collected values into the final line.
Frequently Asked Questions
It sets the functions that the XML parser calls for every opening tag and every closing tag. The start function also receives the attributes of the tag.
The start handler receives the XMLParser, the tag name, and an array of attributes. The end handler receives the XMLParser and the tag name.
XML_OPTION_CASE_FOLDING is on by default. Call xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false) to keep the original case.
Yes. A tag such as <br/> is treated as an opening tag followed right away by a closing tag, so both handlers run.
Add xml_set_character_data_handler() and remember the current tag name in the start handler. The text for that tag then arrives in the character data handler.