kiwilan/php-xml-reader 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

kiwilan/php-xml-reader

Composer 安装命令:

composer require kiwilan/php-xml-reader

包简介

PHP package to read XML with nice API.

关键字:

README 文档

README

Banner with cards catalog picture in background and PHP XML Reader title

php version downloads license tests codecov

PHP package to read XML with nice API, heavily inspired by stackoverflow answer.

About

PHP have native functions to read XML files with SimpleXML, but it's not easy to use and not very flexible. This package offer to read XML files as array, with a simple and flexible API.

Requirements

  • PHP version >= 8.0

Installation

You can install the package via composer:

composer require kiwilan/php-xml-reader

Usage

You can read XML from path or from content.

  • mapContent: boolean If a key has only @content key, return only the value of @content. Default: true.
  • failOnError: boolean Throw exception if XML is invalid. Default: true.
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml', bool $mapContent = true, bool $failOnError = true);

Methods

Method Description Type
$xml->getRoot() Value of root element ?string
$xml->getRootNS() Namespaces of root element string[]
$xml->getRootAttributes() Attributes of root element array<string, mixed>
$xml->getRootAttributes('key'); Value of key attribute of root element mixed
$xml->getVersion(); XML version ?string
$xml->getEncoding(); XML encoding ?string
$xml->isValidXml(); Check if XML is valid bool
$xml->getPath(); Path of XML file ?string
$xml->getFilename(); Filename of XML file ?string
$xml->getConverter(); Converter used to convert XML to array \Kiwilan\XmlReader\XmlConverter
$xml->save('path/to/file.xml'); Save XML file bool
$xml->toArray(); Convert XML to array array<string, mixed>
$xml->__toString(); Convert XML to string string

Advanced methods

Method Description Type Options
$xml->getContents() XML as multidimensional array array<string, mixed>
$xml->find('key') Find key will return first value where that contain key array<string, mixed> strict, content, attributes
$xml->search('key') Search will return all values that contain key mixed
$xml->extract(...keys) Extract metadata key, if not found return null mixed
XmlReader::parseContent(key) Parse content of entry mixed
XmlReader::parseAttributes(key) Parse attributes of entry array<string, mixed> or null

Basic usage

XML as multidimensional array from root (safe).

use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$contents = $xml->getContents();

Basic usage.

use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$contents = $xml->getContents();
$title = $contents['metadata']['dc:title'] ?? null;

Find

Find key will return first value where key that contain title (safe).

use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$title = $xml->find('title', strict: false);

Find key will return first value where key is dc:title (safe)

use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$dcTitle = $xml->find('dc:title');

Find key will return first value where key that contain dc:title and return @content (safe)

use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$dcCreator = $xml->find('dc:creator', content: true);

Find key will return first value where key contain dc:creator and return @attributes (safe)

use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$dcCreator = $xml->find('dc:creator', attributes: true);

Search

Search will return all values that contain dc (safe)

use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$dc = $xml->search('dc');

Extract

Extract metadata key, if not found return null (safe)

use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$rootKey = $xml->extract('metadata');

Extract metadata and dc:title keys (safe)

use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$subSubKey = $xml->extract(['metadata', 'dc:title']);

Get content

If you want to extract only @content you could use parseContent() method, if you want to extract only @attributes you could use parseAttributes() method.

Extract dc:title key and return @content (safe)

use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$title = $xml->extract(['metadata', 'dc:title']);
$title = XmlReader::parseContent($title);

Extract dc:creator key and return @content (safe)

use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$creator = $xml->extract(['metadata', 'dc:creator']);
$creator = XmlReader::parseContent($creator);

Extract dc:creator key and return @attributes (safe)

use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$creatorAttributes = $xml->extract(['metadata', 'dc:creator']);
$creatorAttributes = XmlReader::parseAttributes($creatorAttributes);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

  • spatie for spatie/package-skeleton-php

License

The MIT License (MIT). Please see License File for more information.

kiwilan/php-xml-reader 适用场景与选型建议

kiwilan/php-xml-reader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 147.45k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2023 年 06 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 147.45k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 33
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-19