darkfriend/php7-xml

PHP7 编码/解码 XML 库

2.0.0 2021-02-17 16:44 UTC

This package is auto-updated.

Last update: 2024-09-18 00:31:00 UTC


README

composer require darkfriend/php7-xml

  • 数组转换为 XML (XML::encode())
  • XML 转换为数组 (XML::decode())

其他 PHP 版本

如何安装

composer require darkfriend/php7-xml

数组结构

  • elementName - 基本示例
    • @attributes
      • key => value (数组)
    • cdata - 多行文本
    • value - 文本或树元素
  • elementName - 带附件的示例元素
    • @attributes
      • key => value (数组)
    • value
      • elementName
        • @attributes
          • key => value (数组)
        • value - 文本或树元素
      • elementName
        • @attributes
          • key => value (数组)
        • value - 文本或树元素
  • elementName - 带重复附件的示例元素
    • @attributes
      • key => value (数组)
    • value
      • elementName - 重复元素 (数组)
        • 0
          • @attributes
            • key => value (数组)
          • value - 文本或树元素
        • 1
          • @attributes
            • key => value (数组)
          • value - 文本或树元素
  • elementName - 重复示例元素
    • 0
      • @attributes
        • key => value (数组)
      • value - 文本或树元素
    • 1
      • @attributes
        • key => value (数组)
      • value - 文本或树元素

编码事件

  • afterChild - 添加子元素时执行函数

如何使用

数组转换为 XML (编码)

$array = [
    'bar' => 'value bar',
    'foo' => 'value foo',
    'der' => [
        '@attributes' => [
            'at1' => 'at1val',
            'at2' => 'at2val',
        ],
        'cdata' => 'this is long text',
    ],
    'qpo' => [
        '@attributes' => [
            'test1' => 'valTest',
        ],
        'value' => [
            'sub1' => [
                'value' => [
                    'sub2'=>'val'
                ]
            ],
            'multi-sub2' => [
                [
                    '@attributes' => [
                        'at1' => 'val'
                    ],
                    'value' => [
                        'multi-sub2-sub3' => [
                            'value' => 'value multi-sub2'
                        ]
                    ]
                ],
                [
                    'value' => 'value multi-sub2'
                ],
            ],
        ],
    ],
    'duplicateElement' => [
        [
            '@attributes' => [
                'atDuplicate1' => 'val'
            ],
            'value' => 'valueDuplicateElement1',
        ],
        [
            '@attributes' => [
                'atDuplicate2' => 'val'
            ],
            'value' => 'valueDuplicateElement2',
        ],
        [
            '@attributes' => [
                'atDuplicate3' => 'val'
            ],
            'value' => [
                'subElement' => 'val'
            ],
        ],
    ]
];

echo \darkfriend\helpers\Xml::encode($array);

// example with use events
\darkfriend\helpers\Xml::encode($array, [
    'afterChild' => function($xml, $child, $name, $params) {
        // your code
        return $child;
    },
]);

编码结果

<?xml version="1.0" encoding="utf-8"?>
<root>
    <bar>value bar</bar>
    <foo>value foo</foo>
    <der at1="at1val" at2="at2val">
        <![CDATA[ this is long text ]]>
    </der>
    <qpo test1="valTest">
        <sub1>
            <sub2>val</sub2>
        </sub1>
        <multi-sub2 at1="val">
            <multi-sub2-sub3>value multi-sub2</multi-sub2-sub3>
        </multi-sub2>
        <multi-sub2>value multi-sub2</multi-sub2>
    </qpo>
    <duplicateElement atDuplicate1="val">valueDuplicateElement1</duplicateElement>
    <duplicateElement atDuplicate2="val">valueDuplicateElement2</duplicateElement>
    <duplicateElement atDuplicate3="val">
        <subElement>val</subElement>
    </duplicateElement>
</root>

XML 字符串转换为数组 (解码)

$xml = '<?xml version="1.0" encoding="utf-8"?>
<root>
    <bar>value bar</bar>
    <foo>value foo</foo>
    <der at1="at1val" at2="at2val">
        <![CDATA[ this is long text ]]>
    </der>
    <qpo test1="valTest">
        <sub1>
            <sub2>val</sub2>
        </sub1>
        <multi-sub2 at1="val">
            <multi-sub2-sub3>value multi-sub2</multi-sub2-sub3>
        </multi-sub2>
        <multi-sub2>value multi-sub2</multi-sub2>
    </qpo>
    <duplicateElement atDuplicate1="val">valueDuplicateElement1</duplicateElement>
    <duplicateElement atDuplicate2="val">valueDuplicateElement2</duplicateElement>
    <duplicateElement atDuplicate3="val">
        <subElement>val</subElement>
    </duplicateElement>
</root>';

var_dump(\darkfriend\helpers\Xml::decode($xml));

解码结果

array(5) {
  ["bar"]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(9) "value bar"
    }
  }
  ["foo"]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(9) "value foo"
    }
  }
  ["der"]=>
  array(1) {
    [0]=>
    array(2) {
      ["@attributes"]=>
      array(2) {
        ["at1"]=>
        string(6) "at1val"
        ["at2"]=>
        string(6) "at2val"
      }
      ["value"]=>
      string(17) "this is long text"
    }
  }
  ["qpo"]=>
  array(1) {
    [0]=>
    array(2) {
      ["@attributes"]=>
      array(1) {
        ["test1"]=>
        string(7) "valTest"
      }
      ["value"]=>
      array(2) {
        ["sub1"]=>
        array(1) {
          [0]=>
          array(1) {
            ["value"]=>
            string(0) ""
          }
        }
        ["multi-sub2"]=>
        array(2) {
          [0]=>
          array(1) {
            ["@attributes"]=>
            array(1) {
              ["at1"]=>
              string(3) "val"
            }
          }
          [1]=>
          array(1) {
            ["value"]=>
            string(16) "value multi-sub2"
          }
        }
      }
    }
  }
  ["duplicateElement"]=>
  array(3) {
    [0]=>
    array(2) {
      ["@attributes"]=>
      array(1) {
        ["atDuplicate1"]=>
        string(3) "val"
      }
      ["value"]=>
      string(22) "valueDuplicateElement1"
    }
    [1]=>
    array(2) {
      ["@attributes"]=>
      array(1) {
        ["atDuplicate2"]=>
        string(3) "val"
      }
      ["value"]=>
      string(22) "valueDuplicateElement2"
    }
    [2]=>
    array(2) {
      ["@attributes"]=>
      array(1) {
        ["atDuplicate3"]=>
        string(3) "val"
      }
      ["value"]=>
      array(1) {
        ["subElement"]=>
        array(1) {
          [0]=>
          array(1) {
            ["value"]=>
            string(3) "val"
          }
        }
      }
    }
  }
}

自定义 <?xml ?> 属性和自定义 <root>

$array = [
    'bar' => 'value bar',
    'foo' => 'value foo',
    'der' => [
        '@attributes' => [
            'at1' => 'at1val',
            'at2' => 'at2val',
        ],
        'value' => 'this is long text',
    ],
];

echo \darkfriend\helpers\Xml::encode(
    $array,
    [
        'root' => '<main atExample="atValue"/>', // custom root element with custom attribute atExample
        'prolog' => [
            'attributes' => [
                'version' => '1.0',
                'encoding' => 'utf-8',
            ],
            'elements' => [ // additional elements for prolog
                /*'<?xml-stylesheet type="text/css" href="/style/design"?>'*/
                '<!-- This is a comment --> '
            ], 
        ],
    ]
);
<?xml version="1.0" encoding="utf-8"?>
<!--  This is a comment  -->
<main atExample="atValue">
    <bar>value bar</bar>
    <foo>value foo</foo>
    <der at1="at1val" at2="at2val">this is long text</der>
</main>

自定义根元素

$array = [
    'bar' => 'value bar',
    'foo' => 'value foo',
    'der' => [
        'cdata' => 'this is long text',
        '@attributes' => [
            'at1' => 'at1val',
            'at2' => 'at2val',
        ],
    ]
];

echo \darkfriend\helpers\Xml::encode(
    $array,
    [
        'root' => '<response/>',
    ]
);
<?xml version="1.0" encoding="utf-8"?>
<response>
    <bar>value bar</bar>
    <foo>value foo</foo>
    <der at1="at1val" at2="at2val"><![CDATA[this is long text]]></der>
</response>