raideer / xml-parser
用于IDE工具的PHP编写的XML解析器。生成抽象语法树。
v0.0.3
2024-07-28 22:07 UTC
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.60
- microsoft/tolerant-php-parser: ^0.1.2
- phpunit/phpunit: ^11.2
This package is auto-updated.
Last update: 2024-08-28 22:22:14 UTC
README
这是一个用于IDE工具的PHP编写的XML解析器。它的速度比现有的XML解析器慢得多,但好处是您可以得到一个抽象语法树作为输出。还有一些小的调整,允许有一定的错误容忍度。
解析器的实现受到了微软的Tolerant PHP Parser的极大启发。
安装
通过 Composer
composer require raideer/xml-parser
使用方法
<?php // Require autoload.php generated by composer if haven't already require __DIR__ . '/vendor/autoload.php'; use Raideer\XmlParser\Parser; $parser = new Parser(); $document = $parser->parse('<xml>Hello world!</xml>'); $document->walkDescendantNodesAndTokens(function ($nodeOrToken) { echo json_encode($nodeOrToken) . PHP_EOL; }); // This will be the output of the first node // { // "type": "element", // "children": { // "3": { // "type": "content", // "children": [ // { // "type": "charData", // "children": [], // "tokens": [ // { // "kind": "TEXT", // "value": "Hello world!", // "offset": 5 // } // ] // } // ], // "tokens": [] // } // }, // "tokens": { // "0": { // "kind": "OPEN", // "value": "<", // "offset": 0 // }, // "1": { // "kind": "NAME", // "value": "xml", // "offset": 1 // }, // "2": { // "kind": "CLOSE", // "value": ">", // "offset": 4 // }, // "4": { // "kind": "OPEN", // "value": "<", // "offset": 17 // }, // "5": { // "kind": "SLASH", // "value": "/", // "offset": 18 // }, // "6": { // "kind": "NAME", // "value": "xml", // "offset": 19 // }, // "7": { // "kind": "CLOSE", // "value": ">", // "offset": 22 // } // } // }