azurre/php-array-xml-converter

Array2XML 转换库归功于 lalit.org

0.5.1 2016-11-10 19:10 UTC

This package is auto-updated.

Last update: 2024-09-24 07:42:32 UTC


README

Array2XML 转换库归功于 lalit.org

用法

//create XML
$xml = Array2Xml::createXML('root_node_name', $php_array);
echo $xml->saveXML();

//create Array
$array = Xml2Array::createArray($xml);
print_r($array);

Array2Xml

@xml 示例

// Build the array that should be transformed into a XML object.
$array = [
    'title' => 'A title',
    'body' => [
        '@xml' => '<html><body><p>The content for the news item</p></body></html>',
    ],
];

// Use the Array2XML object to transform it.
$xml = Array2Xml::createXML('news', $array);
echo $xml->saveXML();

这将导致以下结果。

<?xml version="1.0" encoding="UTF-8"?>
<news>
  <title>A title</title>
  <body>
    <html>
      <body>
        <p>The content for the news item</p>
      </body>
    </html>
  </body>
</news>

参考

更完整的参考信息可以在这里找到 http://www.lalit.org/lab/convert-xml-to-array-in-php-xml2array/ http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes/

变更日志

0.5.1

  • 当传入的数组为空时修复了致命错误,修复由拉取请求 #6 完成

0.5.0

  • 为 DOMDocument::load 添加第二个参数到 XML2Array::createArray,例如:LIBXML_NOCDATA
  • 将方法可见性从私有改为受保护以进行重载
  • 合并拉取请求 #5 以添加子 XML
  • 合并拉取请求 #4 以更改方法可见性和为 load 添加第二个参数

0.1.0

  • 初始发布