当前位置:网站首页>PHP XML expat parser
PHP XML expat parser
2022-06-29 02:32:00 【Crooning ~ singing】
XML What is it? ?
XML Used to describe data , The focus is on what the data is .XML The file describes the structure of the data .
stay XML in , There are no predefined tags . You must define your own label .
To learn more about XML Knowledge , Please visit our XML course .
Expat What is it? ?
To read and update - Create and process - One XML file , You need to XML Parser .
There are two basic XML Parser type :
- Tree based parser : This parser puts XML Convert the document into a tree structure . It analyzes the entire document , It also provides access to the elements in the tree , For example, document object model (DOM).
- Event based parser : take XML Documents are treated as a series of events . When a specific event occurs , The parser will call functions to handle .
Expat A parser is an event based parser .
Event based parsers focus on XML The content of the document , Not their structure . Because of that , Event based parsers can access data faster than tree based parsers .
Please look at the following XML fragment :
<from>Jani</from>
The event based parser puts the above XML Reported as a series of three events :
- Start element :from
- Start CDATA part , value :Jani
- Close element :from
above XML Examples contain well formed XML. But this instance is invalid XML, Because there is no document type declaration associated with it (DTD).
However , In the use of Expat When parsing , It makes no difference .Expat It's a parser that doesn't check for validity , Ignore anything DTD.
As an event based 、 Not verified XML Parser ,Expat Fast and lightweight , Perfect for PHP Of Web Applications .
notes :XML Documentation must be well formed , otherwise Expat Will generate an error .
install
XML Expat The parser function is PHP Core components . These functions can be used without installation .
XML file
Below XML The file will be applied in our example :
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
initialization XML Parser
We will be having PHP In the initialization XML Parser , For different XML Event definition processor , And then parse this XML file .
example
<?php
//Initialize the XML parser
$parser=xml_parser_create();
//Function to use at the start of an element
function start($parser,$element_name,$element_attrs)
{
switch($element_name)
{
case "NOTE":
echo "-- Note --<br>";
break;
case "TO":
echo "To: ";
break;
case "FROM":
echo "From: ";
break;
case "HEADING":
echo "Heading: ";
break;
case "BODY":
echo "Message: ";
}
}
//Function to use at the end of an element
function stop($parser,$element_name)
{
echo "<br>";
}
//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 at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
//Free the XML parser
xml_parser_free($parser);
?>
The above code will be output :
-- Note --
To: Tove
From: Jani
Heading: Reminder
Message: Don't forget me this weekend!
working principle :
- adopt xml_parser_create() Function initialization XML Parser
- Create functions that work with different event handlers
- add to xml_set_element_handler() Function to define , Which function is executed when the parser encounters start and end tags
- add to xml_set_character_data_handler() Function to define , Which function is executed when the parser encounters character data
- adopt xml_parse() Function to parse the file "test.xml"
- In case of mistakes , add to xml_error_string() Function XML Error converted to text description
- call xml_parser_free() Function to release the assigned to xml_parser_create() Function memory
边栏推荐
- Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin
- 微信小程序自定义组件
- They all talk about interviews with big factories. When I interview with small factories, I invite people to drink tea?
- 网上联系客户经理办理炒股开户安全吗?
- e.target与e.currentTarget的区别
- String length
- 11 go Foundation: Interface
- Use photoshop2022 to create a wonderful gradient effect for pictures
- 如何用项目甘特图,做好项目汇报
- CTFHub-Web-密码口令-默认口令
猜你喜欢

Learning Tai Chi Maker - mqtt Chapter II (IX) test of this chapter

【Redis】数据介绍 & 通用命令 & String类型

EMC、EMI、EMS的關系
![[redis] sortedset type](/img/7f/f5f1aa603c8994b669d52a435fed7e.png)
[redis] sortedset type

Talk about the copyonwritearraylist of JUC

MySQL的下载和安装

Ctfhub web password weak password

The thinkphp5.1 runtime file has been changed to 777 permission, but cannot be written
![[redis] get to know redis for the first time](/img/02/3c6a7f6ea8c563386a4cd458024728.png)
[redis] get to know redis for the first time

What is the dry goods microservice architecture? What are the advantages and disadvantages?
随机推荐
Have you learned the common SQL interview questions on the short video platform?
Understanding and design of high concurrency
"The first share of endoscope" broke into IPO two times. Last year, it lost 500million yuan. The commercialization of core products is still in doubt | IPO Express
PHP的exec函数
Is there any risk in opening an account for Dongfang fortune stock? Is it safe for Dongfang fortune to open an account
11-Go基础:接口
Wechat campaign auto like
Application of fsockopen function
扁平结构转换为树结构
跨境资讯站
Eliminate the hover effect when the button is disabled
【Redis】SortedSet类型
安装kibana
信息学奥赛一本通 1361:产生数(Produce) | 洛谷 P1037 [NOIP2002 普及组] 产生数
Dialogue with opensea co creation Alex: we still only touch the tip of the iceberg of NFT capability | chain catcher
Which brokerage is safer and more convenient to open a stock account for big smart phones?
Prepare for the Blue Bridge Cup - double pointer, BFS
大三下期末考試
Regular expression (?: pattern)
Tuples of combined data types