定制 edujugon/xml-mapper 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

edujugon/xml-mapper

Composer 安装命令:

composer require edujugon/xml-mapper

包简介

XMLMapper for Laravel and PHP

关键字:

README 文档

README

Are you working with xml data? then this package is for you. This is the simplest API to interact with XML data.

Installation

type in console:

composer require edujugon/xml-mapper

Laravel 5.*

Laravel 5.5 or higher?

Then you don't have to either register or add the alias, this package uses Package Auto-Discovery's feature, and should be available as soon as you install it via Composer.

(Laravel < 5.5) Register the XMLMapper service by adding it to the providers array.

'providers' => array(
    ...
    Edujugon\XMLMapper\Providers\XMLMapperServiceProvider::class
)

(Laravel < 5.5) Let's add the Alias facade, add it to the aliases array.

'aliases' => array(
    ...
    'XMLMapper' => Edujugon\XMLMapper\Facades\XMLMapper::class,
)

Usage samples

$mapper = new Edujugon\XMLMapper\XMLMapper();
$mapper->loadXML($xmlData);

or

$mapper = new Edujugon\XMLMapper\XMLMapper($xmlData);

or with Laravel Facade

$mapper = XMLMapper::loadXML($xmlData);

Don't forget to use the facade use statement at the top of your class: use Edujugon\XMLMapper\Facades\XMLMapper;

Get value

You must know the tags path. Otherwise you should use findValue.

$value = $mapper->getvalue(['first-tag','second-tag','my-tag']);

The above example takes the value of the tag with name my-tag.

If no parameter passed to the method it looks up the value of the first (parent) tag.

Get attribute

You must know the tags path. Otherwise you should use findAttribute.

$att = $mapper->getAttribute('id',['first-tag','second-tag','my-tag']);

The above example returns the value of the id attribute in my-tag.

Get element

Get a new instance of XMLMapper but with the tag-name element as base xml.

$newXmlMapper = $mapper->getElement('tag-name');

Get elements

Get an array of XMLMapper objects based on the tag-name xml element.

$arrayOFXmlMappers = $mapper->getElement('tag-name');

Find value

$value = $mapper->findValue('my-tag');

It looks for the first tag called my-tag and returns its value.

Find attribute

Get the attribute value of a tag.

$att = $mapper->findAttribute('my-att','my-tag');

It looks for the first tag called my-tag, then try to find my-att as attribute and returns its value.

If no tag passed, it takes the first attribute matching the provided attribute name:

$att = $mapper->findAttribute('my-att');

Find attribute by condition

Loop through all elements trying to match the condition/s. When found, returns the value of the provided attribute.

$att = $mapper->findAttributeWhere('my-att',['id'=>1,'dev'=> 'edu',['name','!=','john']])

Check allowed where operators

Find attributes of a tag

Get an object with those attributes as object properties. First it searches the tag and then retrieves the requested attributes.

$obj = $mapper->findAttributes(['att-1','att-2'],'my-tag')

$name = $obj->name;
$dev = $obj->dev;

If no tag provided, it takes the first tag that has those attributes and return the values.

Find attributes by condition

Loop through all elements trying to match the condition/s. When found, returns an object with those attributes as object properties.

$obj = $mapper->findAttributesWhere(['att-1','att-2'],['dev'=> 'edu',['name','!=','john']])

Check allowed where operators

Find all attributes of a tag

Get an array of objects with the tag attributes as properties

$list = $mapper->findAllAttributesOf('tag-name');

Find all attributes of a tag by condition

Get an array of objects with attributes as properties matching the provided tag name and condition.

$list = $mapper->findAllAttributesOfWhere('tag-name',['dev'=> 'edu',['name','!=','john']])

Check allowed where operators

Where operators

Allowed where syntax.

Default:

  • key => value pair. Will be treated as ==
 ['id' => 1,'name' => 'my name']

Custom:

  • !=
  • !==
  • ===
  • contains
  • containsCaseInsensitive
[['name','!=','john'],['id','!=',7]]
[['name','contains','john']]

They can be combined

['id' => 1,['name','!=','john']]

Replace tag names

You can easily replace any tag name of the xml for an easier access.

$mapper->replaceTagName(
    [
        'a10:author' => 'author',
        'a10:name' => 'name',
        'a10:updated' => 'updated'
    ]
);

The above snippet replaces all tags with names matching the keys and sets their values as new tag names. Also updates the underlying object based on the new xml.

Merge a new xml into the existing one

You can easily merge a new xml into the existing one. It sets it as child of the provided tag.

$mapper->mergeXML($newXml, 'desiredParentTag');

Wrap the current xml with a custom tag

$mapper->wrapWith('tagName');

Add attributes

$mapper-addAttributes(['attr1' => 'value1', 'attr2' => 'value2']);
or
$mapper-addAttributes(['attr1' => 'value1', 'attr2' => 'value2'], 'tagName');

Enjoy :)

edujugon/xml-mapper 适用场景与选型建议

edujugon/xml-mapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 66 次下载、GitHub Stars 达 27, 最近一次更新时间为 2017 年 10 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 66
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 27
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 27
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-10-29