turkevich/php-xml-helper

此包的最新版本(dev-master)没有提供许可证信息。

PHP XML Helper

dev-master 2015-06-17 16:12 UTC

This package is not auto-updated.

Last update: 2024-10-02 09:05:56 UTC


README

静态类,用于将XML字符串编码为数组或对象,以及将数组编码为SimpleXMLElement

安装

使用Composer安装

添加到composer.json中

    "require": {
        ...
        "turkevich/php-xml-helper":"dev-master"
    }

做得好!

示例编码

XML::encode(array(
        'book' => array(
            1,
            'page' => 1,
        ),
        'book1' => array(
            'attribute:test' => 2,
            'attribute:test2' => 'testValue',
            'page' => 1,
            'page1' => 'testValue',
        ),
), 'document')->asXML()
结果
<?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::decode($xml);

or

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

To object:
object(stdClass)#5 (2) {
  ["book"]=>
  array(3) {
    ["item"]=>
    string(1) "1"
    ["page"]=>
    string(1) "1"
    [0]=>
    string(13) "
    
    
  "
  }
  ["book1"]=>
  array(4) {
    ["attribute:"]=>
    array(2) {
      ["test"]=>
      string(1) "2"
      ["test2"]=>
      string(9) "testValue"
    }
    ["page"]=>
    string(1) "1"
    ["page1"]=>
    string(9) "testValue"
    [0]=>
    string(13) "
    
    
  "
  }
}

享受吧,朋友们!