search
HomeBackend DevelopmentXML/RSS TutorialXML operation of PHP extension (3) - XML ​​parser usage and related functions



一、XML 元素结构例程

第一个例程缩进显示文档中的开始元素结构。


Example #1 显示 XML 元素结构


<?php$file = "data.xml";$depth = array();
function startElement($parser, $name, $attrs){
global $depth;
for ($i = 0; $i < $depth[$parser]; $i++) 
{
echo "  ";
}
echo "$name\n";
$depth[$parser]++;
}
function endElement($parser, $name){
global $depth;
$depth[$parser]--;
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "r"))) 
{die("could not open XML input");
}
while ($data = fread($fp, 4096)) 
{
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}xml_parser_free($xml_parser);
?>


二、XML 标签映射例程

Example #1 将 XML 映射为 HTML


此例程直接地将 XML 标签映射为 HTML 标签。 在“map_array”中未找到的元素将被忽略。 当然,此例程只针对特定的 XML 文档类型起作用。


<?php$file = "data.xml";
$map_array = array("BOLD"     => "B","EMPHASIS" => "I","LITERAL"  => "TT");
function startElement($parser, $name, $attrs){
global $map_array;
if (isset($map_array[$name])) {
echo "<$map_array[$name]>";
}
}
function endElement($parser, $name){global $map_array;
if (isset($map_array[$name])) {
echo "</$map_array[$name]>";}
}
function characterData($parser, $data){
echo $data;
}
$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in 
$map_arrayxml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) 
{die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));}
}
xml_parser_free($xml_parser);
?>


三、XML 外部实体例程

此例程用于加亮 XML 代码。举例说明如何使用外部实体引用来包含和解析其他文档, 及处理指令是如何被处理的,及判断处理指令所包含代码是否“可信任”的一种方法

用于此例程的 XML 文档位于此例程的下方( 和 )。


Example #1 外部实体例程


<?php$file = "xmltest.xml";
function trustedFile($file){
// 仅信任本地文件
if (!preg_match("@^([a-z]+)\:\/\/@i", $file)&& fileowner($file) == getmyuid()) {return true;}return false;}
function startElement($parser, $name, $attribs){
echo "<<font color=\"#0000cc\">$name</font>";
if (count($attribs)) {
foreach ($attribs as $k => $v) {
echo " <font color=\"#009900\">$k</font>=\"<fontcolor=\"#990000\">$v</font>\"";}}
echo ">";
}
function endElement($parser, $name){
echo "</<font color=\"#0000cc\">$name</font>>";
}
function characterData($parser, $data){
echo "<b>$data</b>";
}
function PIHandler($parser, $target, $data){
switch (strtolower($target)) {case "php":global $parser_file;
// 如何要解析的文档是“可信任”的, 则说明可安全// 地执行其内部的 PHP 代码。否则,显示代码内容。
if (trustedFile($parser_file[$parser])) {
eval($data);
} 
else {printf("Untrusted PHP code: <i>%s</i>",htmlspecialchars($data));
}
break;}}
function defaultHandler($parser, $data){
if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
printf(&#39;<font color="#aa00aa">%s</font>&#39;,htmlspecialchars($data));
} 
else {printf(&#39;<font size="-1">%s</font>&#39;,htmlspecialchars($data));}}
function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId,$publicId) {
if ($systemId) {if (!list($parser, $fp) = new_xml_parser($systemId)) {printf("Could not open entity %s at %s\n", $openEntityNames,$systemId);
return false;}while ($data = fread($fp, 4096)) {if (!xml_parse($parser, $data, feof($fp))) {
printf("XML error: %s at line %d while parsing entity %s\n",xml_error_string(xml_get_error_code($parser)),xml_get_current_line_number($parser), 
$openEntityNames);xml_parser_free($parser);return false;}}xml_parser_free($parser);return true;}return false;}
function new_xml_parser($file){global $parser_file;
$xml_parser = xml_parser_create();xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_set_processing_instruction_handler($xml_parser, "PIHandler");
xml_set_default_handler($xml_parser, "defaultHandler");
xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler");
if (!($fp = @fopen($file, "r"))) {return false;}
if (!is_array($parser_file)) {settype($parser_file, "array");}$parser_file[$xml_parser] = $file;
return array($xml_parser, $fp);}
if (!(list($xml_parser, $fp) = new_xml_parser($file))) {die("could not open XML input");}
echo "<pre class="brush:php;toolbar:false">";
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));}}
echo "
";echo "parse complete\n"; xml_parser_free($xml_parser); ?>

Example #2 xmltest.xml

<?xml  version=&#39;1.0&#39;?>
nbsp;chapter SYSTEM "/just/a/test.dtd" [


]>
<chapter>
 <title>Title &plainEntity;</title>
 <para>
  <informaltable>
   <tgroup>
    <tbody>
     <row><entry>a1</entry><entry>b1</entry><entry>c1</entry></row>
     <row><entry>a2</entry><entry>c2</entry></row>
     <row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
    </tbody>
   </tgroup>
  </informaltable>
 </para>
 &systemEntity;
 <section>
  <title>About this Document</title>
  <para>
   <!-- this is a comment -->
   <?php  echo &#39;Hi!  This is PHP version &#39; . phpversion(); ?>
  </para>
 </section>
</chapter>

此文件包含在  中:


Example #3 xmltest2.xml


<?xml  version="1.0"?>
nbsp;foo [

]>
<foo>
   <element></element>
   &testEnt;
   <?php  echo "This is some more PHP code being executed."; ?>
</foo>

四、XML解析器函数

  • utf8_decode — 将用 UTF-8 方式编码的 ISO-8859-1 字符串转换成单字节的 ISO-8859-1 字符串。

  • utf8_encode — 将 ISO-8859-1 编码的字符串转换为 UTF-8 编码

  • xml_error_string — 获取 XML 解析器的错误字符串

  • xml_get_current_byte_index — 获取 XML 解析器的当前字节索引

  • xml_get_current_column_number — 获取 XML 解析器的当前列号

  • xml_get_current_line_number — 获取 XML 解析器的当前行号

  • xml_get_error_code — 获取 XML 解析器错误代码

  • xml_parse_into_struct — 将 XML 数据解析到数组中

  • xml_parse — 开始解析一个 XML 文档

  • xml_parser_create_ns — 生成一个支持命名空间的 XML 解析器

  • xml_parser_create — 建立一个 XML 解析器

  • xml_parser_free — 释放指定的 XML 解析器

  • xml_parser_get_option — 从 XML 解析器获取选项设置信息

  • xml_parser_set_option — 为指定 XML 解析进行选项设置

  • xml_set_character_data_handler — 建立字符数据处理器

  • xml_set_default_handler — 建立默认处理器

  • xml_set_element_handler — 建立起始和终止元素处理器

  • xml_set_end_namespace_decl_handler — 建立终止命名空间声明处理器

  • xml_set_external_entity_ref_handler — 建立外部实体指向处理器

  • xml_set_notation_decl_handler — 建立注释声明处理器

  • xml_set_object — 在对象中使用 XML 解析器

  • xml_set_processing_instruction_handler — 建立处理指令(PI)处理器

  • xml_set_start_namespace_decl_handler — 建立起始命名空间声明处理器

  • xml_set_unparsed_entity_decl_handler — 建立未解析实体定义声明处理器

以上就是以上就是的内容,更多相关内容请关注PHP中文网(www.php.cn)!的内容,更多相关内容请关注PHP中文网(www.php.cn)!

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
Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

Building Feeds with XML: A Hands-On Guide to RSSBuilding Feeds with XML: A Hands-On Guide to RSSApr 14, 2025 am 12:17 AM

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

Creating RSS Documents: A Step-by-Step TutorialCreating RSS Documents: A Step-by-Step TutorialApr 13, 2025 am 12:10 AM

The steps to create an RSS document are as follows: 1. Write in XML format, with the root element, including the elements. 2. Add, etc. elements to describe channel information. 3. Add elements, each representing a content entry, including,,,,,,,,,,,. 4. Optionally add and elements to enrich the content. 5. Ensure the XML format is correct, use online tools to verify, optimize performance and keep content updated.

XML's Role in RSS: The Foundation of Syndicated ContentXML's Role in RSS: The Foundation of Syndicated ContentApr 12, 2025 am 12:17 AM

The core role of XML in RSS is to provide a standardized and flexible data format. 1. The structure and markup language characteristics of XML make it suitable for data exchange and storage. 2. RSS uses XML to create a standardized format to facilitate content sharing. 3. The application of XML in RSS includes elements that define feed content, such as title and release date. 4. Advantages include standardization and scalability, and challenges include document verbose and strict syntax requirements. 5. Best practices include validating XML validity, keeping it simple, using CDATA, and regularly updating.

From XML to Readable Content: Demystifying RSS FeedsFrom XML to Readable Content: Demystifying RSS FeedsApr 11, 2025 am 12:03 AM

RSSfeedsareXMLdocumentsusedforcontentaggregationanddistribution.Totransformthemintoreadablecontent:1)ParsetheXMLusinglibrarieslikefeedparserinPython.2)HandledifferentRSSversionsandpotentialparsingerrors.3)Transformthedataintouser-friendlyformatsliket

Is There an RSS Alternative Based on JSON?Is There an RSS Alternative Based on JSON?Apr 10, 2025 am 09:31 AM

JSONFeed is a JSON-based RSS alternative that has its advantages simplicity and ease of use. 1) JSONFeed uses JSON format, which is easy to generate and parse. 2) It supports dynamic generation and is suitable for modern web development. 3) Using JSONFeed can improve content management efficiency and user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool