muriloamaral / easyxml
此包最新版本(dev-master)没有可用的许可证信息。
处理XML的Zend Framework 2模块。可以将数组转换为XML,反之亦然。
dev-master
2015-03-26 14:42 UTC
Requires
- php: >=5.3.3
- zendframework/zendframework: 2.*
Requires (Dev)
- phpunit/phpunit: ~4.5
This package is not auto-updated.
Last update: 2024-10-02 07:58:48 UTC
README
处理XML的Zend Framework 2模块。可以将数组转换为XML,反之亦然。
本模块提供了一个服务和ControllerPlugin,可用于将数组转换为XML,反之亦然。
安装
使用composer
-
在您的composer.json文件中添加此项目
"require": { "muriloamaral/easyxml": "dev-master" }
-
现在运行以下命令来让composer下载EasyXML
$ php composer.phar update
安装后
-
在您的
application.config.php文件中启用它。<?php return array( 'modules' => array( // ... 'EasyXML', ), // ... );
用法
您可以通过ServiceManager创建EasyXML的实例,或者使用EasyXML ControllerPlugin在您的控制器中使用它。
ControllerPlugin
--- XML TO ARRAY ---
public function xml2arrayAction()
{
// IT COULD BE EITHER A XML PATH FILE OR A STRING CONTAINING THE XML CONTENT.
$xml = ROOT_PATH . '/data/my-xml-file.xml';
$array = $this->easyXML()->xml2array($xml);
}
--- ARRAY TO XML ---
public function array2xmlAction()
{
$rootNode = 'books';
$body = array(
'@attributes' => array(
'type' => 'fiction'
),
'book' => array(
array(
'@attributes' => array(
'author' => 'George Orwell'
),
'title' => '1984'
),
array(
'@attributes' => array(
'author' => 'Isaac Asimov'
),
'title' => array('@cdata'=>'Foundation'),
'price' => '$15.61'
),
array(
'@attributes' => array(
'author' => 'Robert A Heinlein'
),
'title' => array('@cdata'=>'Stranger in a Strange Land'),
'price' => array(
'@attributes' => array(
'discount' => '10%'
),
'@value' => '$18.00'
)
)
)
);
$xml = $this->easyXML()->array2xml($rootNode, $body);
}
ServiceManager
--- XML TO ARRAY ---
$xmlService = $this->getServiceLocator()->get('EasyXML');
// IT COULD BE EITHER A XML PATH FILE OR A STRING CONTAINING THE XML CONTENT.
$xml = ROOT_PATH . '/data/my-xml-file.xml';
$array = $xmlService->xml2array($xml);
--- ARRAY TO XML ---
$xmlService = $this->getServiceLocator()->get('EasyXML');
$rootNode = 'books';
$body = array(
'@attributes' => array(
'type' => 'fiction'
),
'book' => array(
array(
'@attributes' => array(
'author' => 'George Orwell'
),
'title' => '1984'
),
array(
'@attributes' => array(
'author' => 'Isaac Asimov'
),
'title' => array('@cdata'=>'Foundation'),
'price' => '$15.61'
),
array(
'@attributes' => array(
'author' => 'Robert A Heinlein'
),
'title' => array('@cdata'=>'Stranger in a Strange Land'),
'price' => array(
'@attributes' => array(
'discount' => '10%'
),
'@value' => '$18.00'
)
)
)
);
$xml = $xmlService->array2xml($rootNode, $body);