Home >Backend Development >PHP Tutorial >Read xml data with php_PHP tutorial

Read xml data with php_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:04:16939browse

Summary: I 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 need to convert xml data into html and present it to everyone. Since the project is based on PHP, I abandoned using JS to read XML and chose to continue using PHP. However, I have never made such an attempt before, so I searched a lot of online information and consulted the PHP work manual. I found that using the parser function in the PHP4 environment is a better choice (of course you can also use DOM, but you need to The server is reconfigured and php5 supports DOM better).
Although I have never come across this kind of problem before, it was solved quickly. However, during the process of solving and exploring, I found that although there is a lot of information about this kind of information on the Internet, it is uneven, and many descriptions are not very detailed. The operation manual is more useful.
Okay, let’s get down to business:
parser is a parser built into PHP to process xml. Its work consists of three events: start tag, read data, and end tag.
That is to say, when processing xml, whenever the start tag, data and end tag are encountered, the function will take corresponding actions to complete the conversion of xml data.
Introduction to functions related to xml reading in php:
Quote:
-------------------------- -------------------------------------------------- ---
Object XML parsing function description
Element xml_set_element_handler() The beginning and end of the element
Character data xml_set_character_data_handler() The beginning of character data
External entity xml_set_external_entity_ref_handler() The external entity appears
Not Parsing external entities xml_set_unparsed_entity_decl_handler() The occurrence of unresolved external entities
Processing instruction xml_set_processing_instruction_handler() The occurrence of processing instructions
The occurrence of notation declaration xml_set_notation_decl_handler() The occurrence of notation declaration
Default xml_set_default_handler() Others do not specify handler functions Event
------------------------------------------------ ----------------------------------
Let me give you a small example using parser. Function to read xml data:
$parser = xml_parser_create(); //Create a parser editor
xml_set_element_handler($parser, "startElement", "endElement");// The corresponding functions when setting up tag triggers are startElement and endElenment respectively
xml_set_character_data_handler($parser, "characterData");//The corresponding functions when setting up data reading
$xml_file="1.xml";// Specify the xml file to be read, which can be url

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445205.htmlTechArticleAbstract: I encountered a problem at work today. Because our project data is too small, we need to be seconded from web search. For data, they only provide us with an xml interface. Therefore, we...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn