当前位置:网站首页>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
边栏推荐
猜你喜欢

微信小程序自定义组件

Day10 enumeration class and annotation

CTFHub-Web-密码口令-默认口令

Koa quick start

【無標題】

Quelques tests pour compléter l'environnement wasm

项目研发,有哪些好用的免费脑图工具软件

How to use project Gantt chart to make project report

Project R & D, what are the free brain mapping tools that are easy to use

矩阵特征值和特征向量求解——特征值分解(EVD)
随机推荐
leetcode 统计无向图中无法互相到达点对数
對補wasm環境的一些測試
网上联系客户经理办理炒股开户安全吗?
Is there any risk in opening an account for Dongfang fortune stock? Is it safe for Dongfang fortune to open an account
Koa 快速入門
【Redis】Hash类型
东方财富股票开户是会有什么风险吗?东方财富开户安全吗
微信小程序自定义组件
哪个证券公司最大最安全 哪家券商服务好
Which brokerage is safer and more convenient to open an account for compass mobile stock?
组合数据类型之元组小练习
Oracle Recovery Tools实战批量坏块修复
Use code binding DataGridView control to display tables in program interface
Talk about the copyonwritearraylist of JUC
Wechat campaign auto like
大智慧手机股票开户哪个券商更安全更方便?
They all talk about interviews with big factories. When I interview with small factories, I invite people to drink tea?
table通过伪类实现 另类自适应
EMC、EMI、EMS的關系
"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