定制 dbrisinajumi/array2xml 二次开发

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

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

dbrisinajumi/array2xml

Composer 安装命令:

composer require dbrisinajumi/array2xml

包简介

Array -> XML Converter Class

关键字:

README 文档

README

Array2xml is a PHP library that converts array to valid XML.

Based on XMLWriter.

Requirements

  • PHP 5.3+
  • XMLWriter

Installation

require_once ('/path/to/array2xml.php');

Usage (Ex.: RSS Last News)

Load the library and set custom configuration:

$array2xml = new Array2xml();
$array2xml->setRootName('rss');
$array2xml->setRootAttrs(array('version' => '2.0'));
$array2xml->setCDataKeys(array('description'));

Start by creating a root element:

$data['channel']['title']        = 'News RSS';
$data['channel']['link']         = 'http://yoursite.com/';
$data['channel']['description']  = 'Amazing RSS News';
$data['channel']['language']     = 'en';

Now pass elements from DB query in cycle:

$row = $db->lastNews();
foreach($row as $key => $lastNews)
{
	$data['channel'][$key]['item']['title']       = $lastNews->title;
	$data['channel'][$key]['item']['link']        = 'http://yoursite.com/news/'.$lastNews->url;
	$data['channel'][$key]['item']['description'] = $lastNews->description;
	$data['channel'][$key]['item']['pubDate']     = date(DATE_RFC1123, strtotime($lastNews->added));
}

You can also set element attributes individually. The example below appends an attribute AttributeName to item node:

$data['channel'][$key]['item']['@attributes'] 		= array('AttributeName' => $attributeValue);

Or, if your node doesn't have children, you can use this:

$data['channel]['@attributes'] = array('AttributeName' => $attributeValue);
$data['channel]['@content']    = 'Content of channel node';

This will set both attributes and the content of channel node.

Alternatively, you can use setElementsAttrs() method:

$array2xml->setElementsAttrs( array('ElementName' => array('AttributeName' => $attributeValue) ));

Note that in this case all elements with specified names will have identical attribute names and values.

If you need to include a raw XML tree somewhere, mark it's element using $array2xml->setRawKeys(array('elementName'))

Finally, convert and print output data to screen

echo $array2xml->convert($data);

Configuration

You can easily configure this lib to fit your specific use case using setters described below.

setVersion(string $version)

Sets XML version header.

setEncoding(string $encoding)

Sets XML encoding

setRootName(string $rootName)

Set XML Root Element Name

setRootAttrs(array $rootAttrs)

Set XML Root Element Attributes

setElementsAttrs(array $attrs)

Set Attributes of every XML Elements that matches the given names. Example argument: ['elementName' => ['someAttr' => 'attrValue']]

setCDataKeys(array $elementNames)

Marking given elements as CData ones

setRawKeys(array $elementNames)

Marking given elements as raw ones

setNumericTagPrefix(string $prefix)

Set default prefix for numeric nodes

setSkipNumeric(bool $skipNumeric)

On/Off Skip numeric nodes

setEmptyElementSyntax(const)

In some cases you might want to control the exact syntax of empty elements.

By default, nodes that are empty or equal to null are using self-closing syntax(<foo/>).

You can override this behavior using Array2xml::EMPTY_FULL to force using closing tag(<foo></foo>).

Available agruments are Array2xml::EMPTY_SELF_CLOSING or Array2xml::EMPTY_FULL

setFilterNumbersInTags(bool|array $data)

Remove numbers from element names.

Possible args are:

  • boolean TRUE to remove numbers from ALL elements
  • array contains node names that need filtering.

This is a easy workaround to have identically named elements in your XML built from an array.

For example, let's build an XML with 3 image nodes:

//list of our images
$images = array('image1.jpg', 'image3.jpg', 'image3.jpg');
// input array
$data = array(); 
for($i=0;$i<3;$i++){
	$data['image'.$i] = $images[$i];
}
$array2xml = new array2xml();
$array2xml->setFilterNumbersInTags(array('image'));

$xml = array2xml->convert($data);

That's it! Now we have a nasty XML with 3 identically named nodes in it.

Testing

phpunit Tests/Array2xmlTest.php

dbrisinajumi/array2xml 适用场景与选型建议

dbrisinajumi/array2xml 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 919 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 03 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 dbrisinajumi/array2xml 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 4
  • Forks: 21
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2015-03-18