承接 sbwerewolf/xml-navigator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sbwerewolf/xml-navigator

Composer 安装命令:

composer require sbwerewolf/xml-navigator

包简介

XmlExtractKit for PHP: Stream large XML, extract only what matters, and get plain PHP arrays.

README 文档

README

Packagist Version Packagist Downloads PHP 8.4+ Static Analysis Test Coverage

XmlExtractKit for PHP: Stream large XML, extract only what matters, and get plain PHP arrays.

large XML → selected nodes → plain PHP arrays

Installation

composer require sbwerewolf/xml-navigator

For local test and coverage dependencies on a standard PHP 8.4 setup, see tests/ENVIRONMENT.md.

Why this package?

XmlExtractKit is built for the boring XML jobs that show up in real systems:

  • convert XML into native PHP arrays;
  • stream huge XML files and extract only the elements you need;
  • keep application code working with plain arrays instead of cursor-level XMLReader logic.

Use it for feeds, partner exports, imports, SOAP-ish payloads, marketplace catalogs, ETL pipelines, and other legacy XML integrations.

Core workflow

Open large XML with XMLReader, select matching nodes, receive plain PHP arrays.

use SbWereWolf\XmlNavigator\Parsing\FastXmlParser;

require_once __DIR__ . '/vendor/autoload.php';

$uri = tempnam(sys_get_temp_dir(), 'xml-extract-kit-');
file_put_contents(
    $uri,
    <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<catalog generated_at="2026-04-05T10:00:00Z">
  <offer id="1001" available="true">
    <name>Keyboard</name>
    <price currency="USD">49.90</price>
  </offer>
  <service id="s-1">
    <name>Warranty</name>
  </service>
  <offer id="1002" available="false">
    <name>Mouse</name>
    <price currency="USD">19.90</price>
  </offer>
</catalog>
XML
);

$reader = XMLReader::open($uri);
foreach (
    FastXmlParser::extractHierarchy(
        $reader,
        static fn(XMLReader $cursor): bool =>
            $cursor->nodeType === XMLReader::ELEMENT
            && $cursor->name === 'offer'
    ) as $offer
) {
    var_export($offer);
    echo PHP_EOL;
}
$reader->close();

unlink($uri);

Output:

array (
  'n' => 'offer',
  'a' =>
  array (
    'id' => '1001',
    'available' => 'true',
  ),
  's' =>
  array (
    0 =>
    array (
      'n' => 'name',
      'v' => 'Keyboard',
    ),
    1 =>
    array (
      'n' => 'price',
      'v' => '49.90',
      'a' =>
      array (
        'currency' => 'USD',
      ),
    ),
  ),
)
array (
  'n' => 'offer',
  'a' =>
  array (
    'id' => '1002',
    'available' => 'false',
  ),
  's' =>
  array (
    0 =>
    array (
      'n' => 'name',
      'v' => 'Mouse',
    ),
    1 =>
    array (
      'n' => 'price',
      'v' => '19.90',
      'a' =>
      array (
        'currency' => 'USD',
      ),
    ),
  ),
)

Index

Working examples

Turn XML into arrays with custom keys

Use XmlConverter when your project already has its own internal array contract and you want hierarchy output with your own key names.

use SbWereWolf\XmlNavigator\Conversion\XmlConverter;

require_once __DIR__ . '/vendor/autoload.php';

$converter = new XmlConverter(
    val: 'value',
    attr: 'attributes',
    name: 'name',
    seq: 'children',
);

$hierarchy = $converter->toHierarchyOfElements(
    '<price currency="USD">129.90</price>'
);

var_export($hierarchy);

Output:

array (
  'name' => 'price',
  'value' => '129.90',
  'attributes' =>
  array (
    'currency' => 'USD',
  ),
)

Extract only the needed elements from large XML without loading the whole document

Use FastXmlParser on top of XMLReader when the file is large and only some nodes matter.

use SbWereWolf\XmlNavigator\Parsing\FastXmlParser;

require_once __DIR__ . '/vendor/autoload.php';

$uri = tempnam(sys_get_temp_dir(), 'xml-extract-kit-');
file_put_contents(
    $uri,
    <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <offer id="1001">
    <name>Keyboard</name>
    <price>49.90</price>
  </offer>
  <service id="s-1">
    <name>Warranty</name>
  </service>
  <offer id="1002">
    <name>Mouse</name>
    <price>19.90</price>
  </offer>
</catalog>
XML
);

$reader = XMLReader::open($uri);

$offers = FastXmlParser::extractHierarchy(
    $reader,
    static fn(XMLReader $cursor):
    bool => $cursor->nodeType === XMLReader::ELEMENT
        && $cursor->name === 'offer'
);

$reader->close();
unlink($uri);

foreach ($offers as $offer) {
    var_export($offer);
    echo PHP_EOL;
}

Convert XML to a traversable array and walk it with XmlElement

Use FastXmlToArray::convert() when you want a stable normalized structure, then wrap it with XmlElement for convenient traversal.

use SbWereWolf\XmlNavigator\Conversion\FastXmlToArray;
use SbWereWolf\XmlNavigator\Navigation\XmlElement;

require_once __DIR__ . '/vendor/autoload.php';

$xml = <<<'XML'
<catalog region="eu">
  <offer id="1001" available="true">
    <name>Keyboard</name>
    <tag>office</tag>
    <tag>usb</tag>
  </offer>
</catalog>
XML;

$root = new XmlElement(FastXmlToArray::convert($xml));
$offer = $root->pull('offer')->current();

echo $root->name() . PHP_EOL;                // catalog
echo $root->get('region') . PHP_EOL;         // eu
echo ($root->hasElement('offer') ? 'yes' : 'no') . PHP_EOL; // yes

echo PHP_EOL;
echo 'offer attributes:' . PHP_EOL;
foreach ($offer->attributes() as $attribute) {
    echo $attribute->name() . '=' . $attribute->value() . PHP_EOL;
}

echo PHP_EOL;
echo 'offer elements with name `tag`:' . PHP_EOL;
$tagValues = array_map(
    static fn (XmlElement $tag): string => $tag->value(),
    $offer->elements('tag')
);

var_export($tagValues);

Output:

catalog
eu
yes

offer attributes:
id=1001
available=true

value of offer elements with name `tag`:
array (
  0 => 'office',
  1 => 'usb',
)

Practical notes

  • attributes are always strings;
  • repeated child tags become indexed arrays in readable output;
  • empty elements become empty arrays in readable output and name-only nodes in normalized output;
  • for one-shot conversion, provide either $xmlText or $xmlUri, but not both;
  • if you already have an XMLReader, use the streaming API first instead of loading the entire document.

Detailed documentation

The detailed method-by-method documentation stays available in dedicated files:

Standalone runnable snippets are also included in examples/.

Common use cases

  • supplier and marketplace feeds;
  • partner imports and exports;
  • ETL jobs that consume XML in batches;
  • SOAP-ish or legacy integration payloads;
  • queue payload preparation and JSON serialization;
  • large catalogs where only selected nodes are relevant.

What it is not

XmlExtractKit is not trying to be:

  • a full XML query language;
  • an XML schema validator;
  • an XML editor;
  • an object mapper that hides XML structure behind a large abstraction layer.

The value proposition is much simpler:

stream XML, extract only what matters, and keep working with plain arrays.

Pick your entry point

Need Start here
I need plain arrays from XML now FastXmlToArray::prettyPrint()
I need a stable normalized structure for traversal FastXmlToArray::convert()
I need to stream only matching elements from large XML FastXmlParser::extractPrettyPrint()
I need streaming plus normalized output FastXmlParser::extractHierarchy()
I need custom key names XmlConverter or XmlParser
I need low-level composition around an existing cursor PrettyPrintComposer or HierarchyComposer

Contacts

Nicholas Volkhin
e-mail ulfnew@gmail.com
phone +7-902-272-65-35
Telegram @sbwerewolf

sbwerewolf/xml-navigator 适用场景与选型建议

sbwerewolf/xml-navigator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.04k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2022 年 01 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「xml」 「streaming」 「integration」 「XMLReader」 「etl」 「xml-to-array」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sbwerewolf/xml-navigator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sbwerewolf/xml-navigator 我们能提供哪些服务?
定制开发 / 二次开发

基于 sbwerewolf/xml-navigator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2022-01-01