kfosoft/php-xml-helper

此包的最新版本(1.0.1)没有可用的许可信息。

PHP XML Helper

1.0.1 2015-09-25 20:35 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:31:29 UTC


README

类用于将 XML 字符串编码为数组或对象,并将数组编码为 SimpleXMLElement

安装

使用 Composer 安装

运行以下命令之一

    php composer.phar require --prefer-dist kfosoft/php-xml-helper:"*"

或在 composer.json 中添加以下内容

    "require": {
        ...
        "kfosoft/php-xml-helper":"*"
    }

做得好!

示例编码

$xml = (new XML())->encode(array(
        'book' => array(
            1,
            'page' => 1,
        ),
        'book1' => array(
            'attribute:test' => 2,
            'attribute:test2' => 'testValue',
            'page' => 1,
            'page1' => 'testValue',
        ),
), 'document')
结果
<?xml version="1.0"?>
<document><book><item>1</item><page>1</page></book><book1 test="2" test2="testValue"><page>1</page><page1>testValue</page1></book1></document>

示例解码

$xml = '<?xml version="1.0"?>
<document><book><item>1</item><page>1</page></book><book1 test="2" test2="testValue"><page>1</page><page1>testValue</page1></book1></document>';

$xml = (new XML())->decode($xml);

or

$xml = (new XML())->decode($xml,true);
结果
To array:
array(2) {
  'book' =>
  array(2) {
    [0] =>
    string(1) "1"
    'page' =>
    string(1) "1"
  }
  'book1' =>
  array(3) {
    'attribute:' =>
    array(2) {
      'test' =>
      string(1) "2"
      'test2' =>
      string(9) "testValue"
    }
    'page' =>
    string(1) "1"
    'page1' =>
    string(9) "testValue"
  }
}


To object:
class stdClass#62 (2) {
  public $book =>
  array(2) {
    [0] =>
    string(1) "1"
    'page' =>
    string(1) "1"
  }
  public $book1 =>
  array(3) {
    'attribute:' =>
    array(2) {
      'test' =>
      string(1) "2"
      'test2' =>
      string(9) "testValue"
    }
    'page' =>
    string(1) "1"
    'page1' =>
    string(9) "testValue"
  }
}

享受吧,朋友们!