php 分析器,php解析器是什么

13b9fba086f0181f685e7e461689b902.png

内建的 Expat 解析器使在 PHP 中处理 XML 文档成为可能。

什么是 XML?

XML 用于描述数据,其焦点是数据是什么。XML 文件描述了数据的结构。

在 XML 中,没有预定义的标签。您必须定义自己的标签。

什么是 Expat?

如需读取和更新 - 创建并处理 - 一个 XML 文档,您需要 XML 解析器。

有两种基本的 XML 解析器类型:

·基于树的解析器:这种解析器把 XML 文档转换为树型结构。它分析整篇文档,并提供了 API 来访问树种的元素,例如文档对象模型 (DOM)。

·基于事件的解析器:将 XML 文档视为一系列的事件。当某个具体的事件发生时,解析器会调用函数来处理。

Expat 解析器是基于事件的解析器。

基于事件的解析器集中在 XML 文档的内容,而不是它们的结果。正因如此,基于事件的解析器能够比基于树的解析器更快地访问数据。

请看下面的 XML 片段:John

基于事件的解析器把上面的 XML 报告为一连串的三个事件:

·开始元素:from

·开始 CDATA 部分, 值:John

·关闭元素: from

上面的 XML 范例包含了形式良好的 XML。不过这个例子是无效的 XML,因为没有与它关联的文档类型声明 (DTD),也没有内嵌的 DTD。

相关推荐:《php入门教程》

不过,在使用 Expat 解析器时,这没有区别。Expat 是不检查有效性的解析器,忽略任何 DTD。

作为一款基于事件、非验证的 XML 解析器,Expat 快速且轻巧,十分适合 PHP 的 web 应用程序。

注释:XML 文档必须形式良好,否则 Expat 会生成错误。

安装

XML Expat 解析器是 PHP 核心的组成部分。无需安装就可以使用这些函数。

XML 文件

将在我们的例子中使用下面的 XML 文件:<?xml version="1.0" encoding="ISO-8859-1"?>

George

John

Reminder

初始化 XML 解析器

我们要在 PHP 中初始化 XML 解析器,为不同的 XML 事件定义处理器,然后解析这个 XML 文件。

例子:<?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 --
";

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 "
";

}

//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);

?>

以上代码的输出:-- Note --

To: George

From: John

Heading: Reminder

Message: Don't forget the meeting!

工作原理解释:

·通过 xml_parser_create() 函数初始化 XML 解析器

·创建配合不同事件处理程序的的函数

·添加 xml_set_element_handler() 函数来定义,当解析器遇到开始和结束标签时执行哪个函数

·添加 xml_set_character_data_handler() 函数来定义,当解析器遇到字符数据时执行哪个函数

·通过 xml_parse() 函数来解析文件 "test.xml"

·万一有错误的话,添加 xml_error_string() 函数把 XML 错误转换为文本说明

·调用 xml_parser_free() 函数来释放分配给 xml_parser_create() 函数的内存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个早期的 PHP 解析器,相当于实现了 PHPPHP 脚本的解析。示例代码:<?php // Autoload required classes require "vendor/autoload.php"; // Instantiate new parser instance $parser = new PhpParser\Parser(); // Return and print an AST from string contents $astNode = $parser->parseSourceFile('<?php /* comment */ echo "hi!"'); var_dump($astNode); // Gets and prints errors from AST Node. The parser handles errors gracefully, // so it can be used in IDE usage scenarios (where code is often incomplete). $errors = PhpParser\Utilities::getDiagnostics($astNode); var_dump(iterator_to_array($errors)); // Traverse all Node descendants of $astNode foreach ($astNode->getDescendantNodes() as $descendant) {     if ($descendant instanceof \PhpParser\Node\StringLiteral) {         // Print the Node text (without whitespace or comments)         var_dump($descendant->getText());         // All Nodes link back to their parents, so it's easy to navigate the tree.         $grandParent = $descendant->getParent()->getParent();         var_dump($grandParent->getNodeKindName());         // The AST is fully-representative, and round-trippable to the original source.         // This enables consumers to build reliable formatting and refactoring tools.         var_dump($grandParent->getLeadingCommentAndWhitespaceText());     }     // In addition to retrieving all children or descendants of a Node,     // Nodes expose properties specific to the Node type.     if ($descendant instanceof \PhpParser\Node\Expression\EchoExpression) {         $echoKeywordStartPosition = $descendant->echoKeyword->getStartPosition();         // To cut down on memory consumption, positions are represented as a single integer          // index into the document, but their line and character positions are easily retrieved.         $lineCharacterPosition = \PhpParser\Utilities::getLineCharacterPositionFromPosition(             $echoKeywordStartPosition         );         echo "line: $lineCharacterPosition->line, character: $lineCharacterPosition->character";     } } 标签:Tolerant
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值