jackiedo / xml-array
用于处理xml与数组之间转换的简单工具
Requires
- php: >=5.4.0
- ext-dom: *
Requires (Dev)
- phpunit/phpunit: ~5.0
README
xml与数组之间的转换变得比以往任何时候都要简单。本包提供了一些非常简单的类,用于将XML转换为数组及其反向转换。
本包的功能
- 将XML对象(DOMDocument、SimpleXMLElement)或格式良好的XML字符串转换为关联数组或Json字符串。
- 将关联数组转换为格式良好的XML字符串或DOMDocument。
- 支持在转换过程中解析和构建XML的属性、Cdata部分和命名空间。
概述
查看以下任一会话以了解更多关于此包的信息。
安装
您可以通过 Composer 安装此包。
$ composer require jackiedo/xml-array
基本用法
从XML转换
Web有两种以下方法
转换为数组:
Xml2Array::convert(DOMDocument|SimpleXMLElement|string $inputXML)->toArray();
转换为Json:
Xml2Array::convert(DOMDocument|SimpleXMLElement|string $inputXML)->toJson(int $flag = 0);
注意:输入XML可以是DOMDocument对象、SimpleXMLElement对象或格式良好的XML字符串之一。
示例1: - (从XML字符串转换)
use Jackiedo\XmlArray\Xml2Array; ... $xmlString = '<?xml version="1.0" encoding="UTF-8"?> <root_node> <tag>Example tag</tag> <attribute_tag description="This is a tag with attribute">Another tag with attributes</attribute_tag> <cdata_section><![CDATA[ This is CDATA section ]]></cdata_section> <tag_with_subtag> <sub_tag>Sub tag 1</sub_tag> <sub_tag>Sub tag 2</sub_tag> </tag_with_subtag> <mixed_section> Hello <![CDATA[ This is another CDATA section ]]> <section id="sec_1">Section number 1</section> <section id="sec_2">Section number 2</section> <section id="sec_3">Section number 3</section> </mixed_section> <example:with_namespace xmlns:example="http://example.com"> <example:sub>Content</example:sub> </example:with_namespace> </root_node>'; $array = Xml2Array::convert($xmlString)->toArray();
运行此段代码后,$array
将包含以下内容
$array = [ "root_node" => [ "tag" => "Example tag", "attribute_tag" => [ "@value" => "Another tag with attributes", "@attributes" => [ "description" => "This is a tag with attribute" ] ], "cdata_section" => [ "@cdata" => "This is CDATA section" ], "tag_with_subtag" => [ "sub_tag" => ["Sub tag 1", "Sub tag 2"] ], "mixed_section" => [ "@value" => "Hello", "@cdata" => "This is another CDATA section", "section" => [ [ "@value" => "Section number 1", "@attributes" => [ "id" => "sec_1" ] ], [ "@value" => "Section number 2", "@attributes" => [ "id" => "sec_2" ] ], [ "@value" => "Section number 3", "@attributes" => [ "id" => "sec_3" ] ] ] ], "example:with_namespace" => [ "example:sub" => "Content" ], "@attributes" => [ "xmlns:example" => "http://example.com" ] ] ]
示例2: - (从XML对象转换,例如SimpleXMLElement)
use Jackiedo\XmlArray\Xml2Array; ... $xmlObject = simplexml_load_file('https://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx'); $array = Xml2Array::convert($xmlObject)->toArray();
上述代码的结果是
$array = [ "ExrateList" => [ "DateTime" => "11/26/2018 1:56:20 PM", "Exrate" => [ [ "@attributes" => [ "CurrencyCode" => "AUD", "CurrencyName" => "AUST.DOLLAR", "Buy" => "16724.09", "Transfer" => "16825.04", "Sell" => "17008.7" ] ], [ "@attributes" => [ "CurrencyCode" => "CAD", "CurrencyName" => "CANADIAN DOLLAR", "Buy" => "17412.21", "Transfer" => "17570.34", "Sell" => "17762.14" ] ], [ "@attributes" => [ "CurrencyCode" => "CHF", "CurrencyName" => "SWISS FRANCE", "Buy" => "23074.67", "Transfer" => "23237.33", "Sell" => "23538.02" ] ], [ "@attributes" => [ "CurrencyCode" => "DKK", "CurrencyName" => "DANISH KRONE", "Buy" => "0", "Transfer" => "3493.19", "Sell" => "3602.67" ] ], [ "@attributes" => [ "CurrencyCode" => "EUR", "CurrencyName" => "EURO", "Buy" => "26264.39", "Transfer" => "26343.42", "Sell" => "26736.61" ] ], [ "@attributes" => [ "CurrencyCode" => "GBP", "CurrencyName" => "BRITISH POUND", "Buy" => "29562.43", "Transfer" => "29770.83", "Sell" => "30035.68" ] ], [ "@attributes" => [ "CurrencyCode" => "HKD", "CurrencyName" => "HONGKONG DOLLAR", "Buy" => "2939.91", "Transfer" => "2960.63", "Sell" => "3004.95" ] ], [ "@attributes" => [ "CurrencyCode" => "INR", "CurrencyName" => "INDIAN RUPEE", "Buy" => "0", "Transfer" => "331.15", "Sell" => "344.15" ] ], [ "@attributes" => [ "CurrencyCode" => "JPY", "CurrencyName" => "JAPANESE YEN", "Buy" => "200.38", "Transfer" => "202.4", "Sell" => "207.05" ] ], [ "@attributes" => [ "CurrencyCode" => "KRW", "CurrencyName" => "SOUTH KOREAN WON", "Buy" => "19.07", "Transfer" => "20.07", "Sell" => "21.33" ] ], [ "@attributes" => [ "CurrencyCode" => "KWD", "CurrencyName" => "KUWAITI DINAR", "Buy" => "0", "Transfer" => "76615.44", "Sell" => "79621.23" ] ], [ "@attributes" => [ "CurrencyCode" => "MYR", "CurrencyName" => "MALAYSIAN RINGGIT", "Buy" => "0", "Transfer" => "5532.17", "Sell" => "5603.76" ] ], [ "@attributes" => [ "CurrencyCode" => "NOK", "CurrencyName" => "NORWEGIAN KRONER", "Buy" => "0", "Transfer" => "2674.72", "Sell" => "2758.55" ] ], [ "@attributes" => [ "CurrencyCode" => "RUB", "CurrencyName" => "RUSSIAN RUBLE", "Buy" => "0", "Transfer" => "349.9", "Sell" => "389.89" ] ], [ "@attributes" => [ "CurrencyCode" => "SAR", "CurrencyName" => "SAUDI RIAL", "Buy" => "0", "Transfer" => "6206.27", "Sell" => "6449.75" ] ], [ "@attributes" => [ "CurrencyCode" => "SEK", "CurrencyName" => "SWEDISH KRONA", "Buy" => "0", "Transfer" => "2536.35", "Sell" => "2600.19" ] ], [ "@attributes" => [ "CurrencyCode" => "SGD", "CurrencyName" => "SINGAPORE DOLLAR", "Buy" => "16775.66", "Transfer" => "16893.92", "Sell" => "17078.33" ] ], [ "@attributes" => [ "CurrencyCode" => "THB", "CurrencyName" => "THAI BAHT", "Buy" => "691.64", "Transfer" => "691.64", "Sell" => "720.49" ] ], [ "@attributes" => [ "CurrencyCode" => "USD", "CurrencyName" => "US DOLLAR", "Buy" => "23295", "Transfer" => "23295", "Sell" => "23385" ] ] ], "Source" => "Joint Stock Commercial Bank for Foreign Trade of Vietnam - Vietcombank" ] ];
示例3: - (转换为Json)
$jsonString = Xml2Array::convert($xmlString)->toJson(JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
从数组转换
我们还有以下两种方法
转换为XML字符串:
Array2Xml::convert(array $array)->toXml(bool $prettyOutput = false);
转换为DOMDocument对象:
Array2Xml::convert(array $array)->toDom();
示例4:
use Jackiedo\XmlArray\Array2Xml; ... // We will use array from the result of first example as input for this example $xmlString = Array2Xml::convert($array)->toXml(true);
示例5:
$domObject = Array2Xml::convert($array)->toDom();
高级用法
设置配置
如果我们想更改转换过程设置,可以按以下方式操作
方法1
... $config = [ 'valueKey' => '@text', 'cdataKey' => '@cdata-section', ... ]; $array = Xml2Array::convert($inputXml, $config)->toArray(); ... // And for backward processing $xml = Array2Xml::convert($inputArray, $config)->toXml();
注意:配置是参数数组。有关更多详细信息,请参阅默认配置部分。
方法2
$converter = new Xml2Array($config); $array = $converter->convertFrom($inputXml)->toArray();
方法3
$converter = new Xml2Array; $array = $converter->setConfig($config)->convertFrom($inputXml)->toArray();
获取配置
如果我们使用方法2和方法3实现了转换过程,则可以使用该方法获取转换配置
$config = $converter->getConfig();
默认配置
对于Xml2Array
$defaultConfig = [ 'version' => '1.0', // Version of XML document 'encoding' => 'UTF-8', // Encoding of XML document 'standalone' => null, // Standalone directive for XML document 'attributesKey' => '@attributes', // The key name use for storing attributes of node 'cdataKey' => '@cdata', // The key name use for storing value of Cdata Section in node 'valueKey' => '@value', // The key name use for storing text content of node 'namespacesOnRoot' => true // Collapse all the namespaces at the root node, otherwise it will put in the nodes for which the namespace first appeared. ];
对于Array2Xml
$defaultConfig = [ 'version' => '1.0', // Version of XML document 'encoding' => 'UTF-8', // Encoding of XML document 'standalone' => null, // Standalone directive for XML document 'attributesKey' => '@attributes', // The key name use for storing attributes of node 'cdataKey' => '@cdata', // The key name use for storing value of Cdata Section in node 'valueKey' => '@value', // The key name use for storing text content of node 'rootElement' => null, // The name of root node will be create automatically in process of conversion 'keyFixer' => true, // The automatically key normalization will be used during conversion. It can be bool|string|numeric|callable ];
配置设置的影响
版本
影响:此设置允许指定生成的XML版本(在Array2Xml中),或从XML字符串中重建的版本(在Xml2Array中)
编码
影响:此设置用于指示生成的XML的编码类型(在Array2Xml中),或从XML字符串中重建的编码类型(在Xml2Array中)
独立
影响:此设置用于允许在XML中显示或不显示standalone
指令。如果设置为null
,则不会显示此指令。
示例:
$xml = Array2Xml::convert($array, [ 'standalone' => true ])->toXml(true);
$xml中的内容将是
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> ...
attributesKey, cdataKey, valueKey
影响:此设置允许在转换过程中使用特殊关键字来包含属性值、Cdata部分等。
示例:请参阅上面的示例以了解更多详细信息。
namespacesOnRoot
影响:此设置允许收集所有解析的XML命名空间并将它们放置在根节点中。如果设置为false
,则命名空间将位于声明的节点中。
示例:我们再次使用上面的示例 1
,但这次配置不同
$array = Xml2Array::convert($xmlString, [ 'namespacesOnRoot' => false ])->toArray();
运行此段代码后,$array
将包含以下内容
$array = [ "root_node" => [ "tag" => "Example tag", "attribute_tag" => [ "@value" => "Another tag with attributes", "@attributes" => [ "description" => "This is a tag with attribute" ] ], ... "example:with_namespace" => [ "@attributes" => [ "xmlns:example" => "http://example.com" ] "example:sub" => "Content" ], ] ]
你看,xlmns:example
命名空间被放在example:with_namespace
键,而不是像最初那样放在root_node
。
rootElement
效果:根据良好形式的XML标准,XML内容仅允许有一个根节点。此设置允许将原始数组的所有元素包裹在一个根节点中,而不是需要手动编辑您的数组。
keyFixer
效果:
根据良好形式的XML标准,标签名和属性必须满足一系列要求,命名规定如下
- 只能以字母字符和下划线开头。
- 只能接受
[a-zA-Z]
、-
、_
、.
、:
字符。其中,:
用于表示命名空间前缀。 - 不允许以
:
结尾
在转换过程中,违反这些规则的数组键名将自动规范化。如果您不同意这种规范化,请将此设置设置为false
。
默认情况下,此规范化将无效字符替换为下划线(_
)。您可以更改为您喜欢的其他字符。
示例:
// Do not use the key normalization $xml = Array2Xml::convert($array, [ 'keyFixer' => false ])->toXml(); // Use the key normalization with default character (_) $xml = Array2Xml::convert($array, [ 'keyFixer' => true ])->toXml(); // Replace with '---' $xml = Array2Xml::convert($array, [ 'keyFixer' => '---' ])->toXml(); // Use a callable for fixing $xml = Array2Xml::convert($array, [ 'keyFixer' => function ($key) { $key = str_replace('/', '_', $key); $key = str_replace('\\' , '.', $key); return $key; } ])->toXml();
许可
MIT © Jackie Do