Use php to read xml data _ PHP Tutorial

Source: Internet
Author: User
Use php to read xml data. Abstract: I encountered a problem at work today. because our project data is too small, we need to borrow data from websearch. they only provide us with an xml interface. Therefore, our abstract: we encountered a problem at work today. because our project data is too small, we need to borrow data from the web search side. they only provide us with an xml interface. Therefore, we need to convert xml data into html for presentation. Because the project is based on php, we discard js to read xml and choose to continue using php. However, I have never tried this kind of thing before, so I found a lot of online materials and referred to the php work manual, it is found that using the parser function in the php4 environment is a good choice (of course, dom can also be used, but the server needs to be re-configured and php5 supports dom better ).
Although I have never touched on this kind of problem before, I have quickly solved it. However, I found that although there are a lot of such information on the Internet, it is uneven, many descriptions are not very detailed, or the operation manual is quite useful.
Let's get down to the truth:
Parser is a php built-in parser used to process xml. its work consists of three events: start tag, read data, and end tag.
That is to say, when processing xml, the function performs the corresponding action to complete the conversion of xml data whenever the start tag, data, and end tag are encountered.
Introduction to functions related to xml reading in php:
Reference:
--------------------------------------------------------------------------------
Object XML parsing function description
Start and end of element xml_set_element_handler ()
Character data xml_set_character_data_handler () start of character data
External entity xml_set_external_entity_ref_handler () external entity appears
External entity xml_set_unparsed_entity_decl_handler () not resolved external entity appears
Processing Command xml_set_processing_instruction_handler () processing command appears
The emergence of the xml_set_notation_decl_handler () method declaration
By default, xml_set_default_handler () is used for events that do not specify a processing function.
--------------------------------------------------------------------------------
Here is a small example to use the parser function to read xml data:
$ Parser = xml_parser_create (); // Create a parser editor
Xml_set_element_handler ($ parser, "startElement", "endElement"); // corresponding functions for tag setting triggering are startElement and endElenment respectively.
Xml_set_character_data_handler ($ parser, "characterData"); // you can specify a function for reading data.
$ Xml_file = "1.xml"; // specify the xml file to be read, which can be a url

Parse encountered a problem at work today. because our project data is too small, we need to borrow data from web search. they only provide us with an xml interface. Therefore, we...

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.