desperado / xml-bundle
Symfony XmlBundle
0.1.7
2015-04-16 10:04 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-14 16:18:45 UTC
README
Symfony2 的 XML 构建器和读取器扩展包
安装
使用 composer
将 desperado/xml-bundle
添加到您的 composer.json 文件中。
"require": { "desperado/xml-bundle": "dev-master" }
接下来,您应该通过执行以下命令来安装扩展包
php composer.phar update desperado/xml-bundle
最后,将扩展包添加到 app/AppKernel.php 文件中 AppKernel 类的 registerBundles 函数中
public function registerBundles()
{
$bundles = array(
...
new Desperado\XmlBundle\DesperadoXmlBundle,
...
);
使用方法
DIC
- XmlEditor: desperado_xml.model.xml_editor
- XmlGenerator: desperado_xml.model.xml_generator
- XmlReader: desperado_xml.model.xml_reader
- XmlPrepare: desperado_xml.model.xml_prepare
从数组创建 xml
<?php use Desperado\XmlBundle\Model\XmlGenerator; $params = [ 'Request' => [ '@ns' => [ '0_xmlns' => ['prefix' => 'xsi', 'uri' => 'http//www.w3.org/2001/XMLSchema-instance'], '1_xmlns' => ['prefix' => 'xsd', 'uri' => 'http//www.w3.org/2001/XMLSchema'], ], '@attrib' => [ 'Id' => 100, 'Service' => 200, 'xmlns' => 'http://ekassir.com/ekassir/PaySystem/Server/eKassirV3Protocol' ], 'PaymentParameters' => [ '@attrib' => ['xmlns' => ''], 'Parameter' => [ '@attrib' => ['Name' => 'account'], '@value' => 'emptyAccount' ] ] ] ]; $xmlGenerator = new XmlGenerator; echo $xmlGenerator->generateFromArray($params);
输出
<?xml version="1.0" encoding="UTF-8"?> <root xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http//www.w3.org/2001/XMLSchema"> <Request xmlns="http://ekassir.com/ekassir/PaySystem/Server/eKassirV3Protocol" xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http//www.w3.org/2001/XMLSchema" Id="100" Service="200" xsi:xmlns="" xsd:xmlns=""> <PaymentParameters xmlns=""> <Parameter Name="account">emptyAccount</Parameter> </PaymentParameters> </Request> </root>
创建不带属性、命名空间等内容的 XML
<?php use Desperado\XmlBundle\Model\XmlPrepare; use Desperado\XmlBundle\Model\XmlGenerator; $params = [ 'Details' => [ 'PaymentParameters' => [ 'first_node' => 'first_node_value', 'second_node' => 'second_node_value' ] ] ]; $xmlPrepare = new XmlPrepare; $xmlGenerator = new XmlGenerator; echo $xmlGenerator->setRootName('request')->generateFromArray($xmlPrepare->prepareArrayBeforeToXmlConvert($params));
输出
<?xml version="1.0" encoding="UTF-8"?> <request> <Details> <PaymentParameters> <first_node>first_node_value</first_node> <second_node>second_node_value</second_node> </PaymentParameters> </Details> </request>
解析不带属性、命名空间等内容的 XML
<?php use Desperado\XmlBundle\Model\XmlReader; $xmlString = '<?xml version="1.0" encoding="UTF-8"?> <request> <Details> <PaymentParameters> <first_node>first_node_value</first_node> <second_node>second_node_value</second_node> </PaymentParameters> </Details> </request>'; $xmlReader = new XmlReader; print_r($xmlReader->processConvert($xmlString));
输出
Array ( [Details] => Array ( [PaymentParameters] => Array ( [first_node] => first_node_value [second_node] => second_node_value ) ) )