PHP XML Expat Parser
There are two basic types of XML parsers:
Tree-based parser: This parser transforms an XML document into a tree structure. It parses the entire document and provides an API to access the elements of the tree, such as the Document Object Model (DOM).
Event-based parser: treats an XML document as a series of events. When a specific event occurs, the parser invokes the function to process it.
The Expat parser is an event-based parser.
The XML Expat parser is part of the PHP core. These functions can be used without installation.
XML file:
George
John
Reminder
Don ' t forget the meeting!
Initialize the XML parser:
";
Break
Case ' to ':
echo "To:";
Break
Case ' from ':
echo "From:";
Break
Case "HEADING":
echo "Heading:";
Break
Case "BODY":
echo "Message:";
}
}
Function to use in the end of an element
function Stop ($parser, $element _name)
{
echo "
";
}
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 @ line%d",
Xml_error_string (Xml_get_error_code ($parser)),
Xml_get_current_line_number ($parser)));
}
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.