{"id":15296299,"url":"https://github.com/darkfriend/php7-xml","last_synced_at":"2026-01-06T01:06:33.644Z","repository":{"id":56962452,"uuid":"233464864","full_name":"darkfriend/php7-xml","owner":"darkfriend","description":"Generate XML from array. Array to XML. XML encode/decode.","archived":false,"fork":false,"pushed_at":"2021-02-17T17:06:58.000Z","size":28,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-21T22:02:55.359Z","etag":null,"topics":["array","array-to-xml","php","php7","php7-xml","xml","xml-to-array"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/darkfriend.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-12T21:58:37.000Z","updated_at":"2021-02-22T23:34:57.000Z","dependencies_parsed_at":"2022-08-21T05:40:20.919Z","dependency_job_id":null,"html_url":"https://github.com/darkfriend/php7-xml","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darkfriend%2Fphp7-xml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darkfriend%2Fphp7-xml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darkfriend%2Fphp7-xml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darkfriend%2Fphp7-xml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/darkfriend","download_url":"https://codeload.github.com/darkfriend/php7-xml/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245407755,"owners_count":20610232,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","array-to-xml","php","php7","php7-xml","xml","xml-to-array"],"created_at":"2024-09-30T18:09:59.853Z","updated_at":"2026-01-06T01:06:33.618Z","avatar_url":"https://github.com/darkfriend.png","language":"PHP","readme":"# PHP7 library for encode/decode xml\r\n\r\n``composer require darkfriend/php7-xml``\r\n\r\n* Array to XML (``` XML::encode() ```)\r\n* XML to Array (``` XML::decode() ```)\r\n\r\n### Other php version\r\n* [Xml encode/decode for php5](https://github.com/darkfriend/php5-xml)\r\n\r\n## How to install\r\n\r\n```\r\ncomposer require darkfriend/php7-xml\r\n```\r\n\r\n## Array Structure\r\n\r\n* elementName - basic example\r\n    * @attributes\r\n        * `key =\u003e value` (array)\r\n    * cdata - multiline text\r\n    * value - text or tree elements\r\n* elementName - example element with attachments\r\n    * @attributes\r\n        * `key =\u003e value` (array)\r\n    * value\r\n        * elementName\r\n            * @attributes\r\n                * `key =\u003e value` (array)\r\n            * value - text or tree elements\r\n        * elementName\r\n            * @attributes\r\n                * `key =\u003e value` (array)\r\n            * value - text or tree elements\r\n* elementName - example element with duplicate attachments\r\n    * @attributes\r\n        * `key =\u003e value` (array)\r\n    * value\r\n        * elementName - duplicate element (array)\r\n            * 0\r\n                * @attributes\r\n                    * `key =\u003e value` (array)\r\n                * value - text or tree elements\r\n            * 1\r\n                * @attributes\r\n                    * `key =\u003e value` (array)\r\n                * value - text or tree elements\r\n* elementName - example element duplicate\r\n    * 0\r\n        * @attributes\r\n            * `key =\u003e value` (array)\r\n        * value - text or tree elements\r\n    * 1\r\n        * @attributes\r\n            * `key =\u003e value` (array)\r\n        * value - text or tree elements \r\n\r\n### Encode Events\r\n\r\n* afterChild - execute function when added child element\r\n\r\n## How to use\r\n\r\n### Array to XML (encode)\r\n\r\n```php\r\n$array = [\r\n    'bar' =\u003e 'value bar',\r\n    'foo' =\u003e 'value foo',\r\n    'der' =\u003e [\r\n        '@attributes' =\u003e [\r\n            'at1' =\u003e 'at1val',\r\n            'at2' =\u003e 'at2val',\r\n        ],\r\n        'cdata' =\u003e 'this is long text',\r\n    ],\r\n    'qpo' =\u003e [\r\n        '@attributes' =\u003e [\r\n            'test1' =\u003e 'valTest',\r\n        ],\r\n        'value' =\u003e [\r\n            'sub1' =\u003e [\r\n                'value' =\u003e [\r\n                    'sub2'=\u003e'val'\r\n                ]\r\n            ],\r\n            'multi-sub2' =\u003e [\r\n                [\r\n                    '@attributes' =\u003e [\r\n                        'at1' =\u003e 'val'\r\n                    ],\r\n                    'value' =\u003e [\r\n                        'multi-sub2-sub3' =\u003e [\r\n                            'value' =\u003e 'value multi-sub2'\r\n                        ]\r\n                    ]\r\n                ],\r\n                [\r\n                    'value' =\u003e 'value multi-sub2'\r\n                ],\r\n            ],\r\n        ],\r\n    ],\r\n    'duplicateElement' =\u003e [\r\n        [\r\n            '@attributes' =\u003e [\r\n                'atDuplicate1' =\u003e 'val'\r\n            ],\r\n            'value' =\u003e 'valueDuplicateElement1',\r\n        ],\r\n        [\r\n            '@attributes' =\u003e [\r\n                'atDuplicate2' =\u003e 'val'\r\n            ],\r\n            'value' =\u003e 'valueDuplicateElement2',\r\n        ],\r\n        [\r\n            '@attributes' =\u003e [\r\n                'atDuplicate3' =\u003e 'val'\r\n            ],\r\n            'value' =\u003e [\r\n                'subElement' =\u003e 'val'\r\n            ],\r\n        ],\r\n    ]\r\n];\r\n\r\necho \\darkfriend\\helpers\\Xml::encode($array);\r\n\r\n// example with use events\r\n\\darkfriend\\helpers\\Xml::encode($array, [\r\n    'afterChild' =\u003e function($xml, $child, $name, $params) {\r\n        // your code\r\n        return $child;\r\n    },\r\n]);\r\n```\r\n\r\n#### Result encode\r\n\r\n```xml\r\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\r\n\u003croot\u003e\r\n    \u003cbar\u003evalue bar\u003c/bar\u003e\r\n    \u003cfoo\u003evalue foo\u003c/foo\u003e\r\n    \u003cder at1=\"at1val\" at2=\"at2val\"\u003e\r\n        \u003c![CDATA[ this is long text ]]\u003e\r\n    \u003c/der\u003e\r\n    \u003cqpo test1=\"valTest\"\u003e\r\n        \u003csub1\u003e\r\n            \u003csub2\u003eval\u003c/sub2\u003e\r\n        \u003c/sub1\u003e\r\n        \u003cmulti-sub2 at1=\"val\"\u003e\r\n            \u003cmulti-sub2-sub3\u003evalue multi-sub2\u003c/multi-sub2-sub3\u003e\r\n        \u003c/multi-sub2\u003e\r\n        \u003cmulti-sub2\u003evalue multi-sub2\u003c/multi-sub2\u003e\r\n    \u003c/qpo\u003e\r\n    \u003cduplicateElement atDuplicate1=\"val\"\u003evalueDuplicateElement1\u003c/duplicateElement\u003e\r\n    \u003cduplicateElement atDuplicate2=\"val\"\u003evalueDuplicateElement2\u003c/duplicateElement\u003e\r\n    \u003cduplicateElement atDuplicate3=\"val\"\u003e\r\n        \u003csubElement\u003eval\u003c/subElement\u003e\r\n    \u003c/duplicateElement\u003e\r\n\u003c/root\u003e\r\n```\r\n\r\n### Xml string to Array (decode)\r\n\r\n```php\r\n$xml = '\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\r\n\u003croot\u003e\r\n    \u003cbar\u003evalue bar\u003c/bar\u003e\r\n    \u003cfoo\u003evalue foo\u003c/foo\u003e\r\n    \u003cder at1=\"at1val\" at2=\"at2val\"\u003e\r\n        \u003c![CDATA[ this is long text ]]\u003e\r\n    \u003c/der\u003e\r\n    \u003cqpo test1=\"valTest\"\u003e\r\n        \u003csub1\u003e\r\n            \u003csub2\u003eval\u003c/sub2\u003e\r\n        \u003c/sub1\u003e\r\n        \u003cmulti-sub2 at1=\"val\"\u003e\r\n            \u003cmulti-sub2-sub3\u003evalue multi-sub2\u003c/multi-sub2-sub3\u003e\r\n        \u003c/multi-sub2\u003e\r\n        \u003cmulti-sub2\u003evalue multi-sub2\u003c/multi-sub2\u003e\r\n    \u003c/qpo\u003e\r\n    \u003cduplicateElement atDuplicate1=\"val\"\u003evalueDuplicateElement1\u003c/duplicateElement\u003e\r\n    \u003cduplicateElement atDuplicate2=\"val\"\u003evalueDuplicateElement2\u003c/duplicateElement\u003e\r\n    \u003cduplicateElement atDuplicate3=\"val\"\u003e\r\n        \u003csubElement\u003eval\u003c/subElement\u003e\r\n    \u003c/duplicateElement\u003e\r\n\u003c/root\u003e';\r\n\r\nvar_dump(\\darkfriend\\helpers\\Xml::decode($xml));\r\n```\r\n\r\n#### Result decode\r\n\r\n```\r\narray(5) {\r\n  [\"bar\"]=\u003e\r\n  array(1) {\r\n    [0]=\u003e\r\n    array(1) {\r\n      [\"value\"]=\u003e\r\n      string(9) \"value bar\"\r\n    }\r\n  }\r\n  [\"foo\"]=\u003e\r\n  array(1) {\r\n    [0]=\u003e\r\n    array(1) {\r\n      [\"value\"]=\u003e\r\n      string(9) \"value foo\"\r\n    }\r\n  }\r\n  [\"der\"]=\u003e\r\n  array(1) {\r\n    [0]=\u003e\r\n    array(2) {\r\n      [\"@attributes\"]=\u003e\r\n      array(2) {\r\n        [\"at1\"]=\u003e\r\n        string(6) \"at1val\"\r\n        [\"at2\"]=\u003e\r\n        string(6) \"at2val\"\r\n      }\r\n      [\"value\"]=\u003e\r\n      string(17) \"this is long text\"\r\n    }\r\n  }\r\n  [\"qpo\"]=\u003e\r\n  array(1) {\r\n    [0]=\u003e\r\n    array(2) {\r\n      [\"@attributes\"]=\u003e\r\n      array(1) {\r\n        [\"test1\"]=\u003e\r\n        string(7) \"valTest\"\r\n      }\r\n      [\"value\"]=\u003e\r\n      array(2) {\r\n        [\"sub1\"]=\u003e\r\n        array(1) {\r\n          [0]=\u003e\r\n          array(1) {\r\n            [\"value\"]=\u003e\r\n            string(0) \"\"\r\n          }\r\n        }\r\n        [\"multi-sub2\"]=\u003e\r\n        array(2) {\r\n          [0]=\u003e\r\n          array(1) {\r\n            [\"@attributes\"]=\u003e\r\n            array(1) {\r\n              [\"at1\"]=\u003e\r\n              string(3) \"val\"\r\n            }\r\n          }\r\n          [1]=\u003e\r\n          array(1) {\r\n            [\"value\"]=\u003e\r\n            string(16) \"value multi-sub2\"\r\n          }\r\n        }\r\n      }\r\n    }\r\n  }\r\n  [\"duplicateElement\"]=\u003e\r\n  array(3) {\r\n    [0]=\u003e\r\n    array(2) {\r\n      [\"@attributes\"]=\u003e\r\n      array(1) {\r\n        [\"atDuplicate1\"]=\u003e\r\n        string(3) \"val\"\r\n      }\r\n      [\"value\"]=\u003e\r\n      string(22) \"valueDuplicateElement1\"\r\n    }\r\n    [1]=\u003e\r\n    array(2) {\r\n      [\"@attributes\"]=\u003e\r\n      array(1) {\r\n        [\"atDuplicate2\"]=\u003e\r\n        string(3) \"val\"\r\n      }\r\n      [\"value\"]=\u003e\r\n      string(22) \"valueDuplicateElement2\"\r\n    }\r\n    [2]=\u003e\r\n    array(2) {\r\n      [\"@attributes\"]=\u003e\r\n      array(1) {\r\n        [\"atDuplicate3\"]=\u003e\r\n        string(3) \"val\"\r\n      }\r\n      [\"value\"]=\u003e\r\n      array(1) {\r\n        [\"subElement\"]=\u003e\r\n        array(1) {\r\n          [0]=\u003e\r\n          array(1) {\r\n            [\"value\"]=\u003e\r\n            string(3) \"val\"\r\n          }\r\n        }\r\n      }\r\n    }\r\n  }\r\n}\r\n```\r\n\r\n### Custom \\\u003c?xml \\?\u003e attributes and custom \\\u003croot\\\u003e\r\n\r\n```php\r\n$array = [\r\n    'bar' =\u003e 'value bar',\r\n    'foo' =\u003e 'value foo',\r\n    'der' =\u003e [\r\n        '@attributes' =\u003e [\r\n            'at1' =\u003e 'at1val',\r\n            'at2' =\u003e 'at2val',\r\n        ],\r\n        'value' =\u003e 'this is long text',\r\n    ],\r\n];\r\n\r\necho \\darkfriend\\helpers\\Xml::encode(\r\n    $array,\r\n    [\r\n        'root' =\u003e '\u003cmain atExample=\"atValue\"/\u003e', // custom root element with custom attribute atExample\r\n        'prolog' =\u003e [\r\n            'attributes' =\u003e [\r\n                'version' =\u003e '1.0',\r\n                'encoding' =\u003e 'utf-8',\r\n            ],\r\n            'elements' =\u003e [ // additional elements for prolog\r\n                /*'\u003c?xml-stylesheet type=\"text/css\" href=\"/style/design\"?\u003e'*/\r\n                '\u003c!-- This is a comment --\u003e '\r\n            ], \r\n        ],\r\n    ]\r\n);\r\n```\r\n\r\n```xml\r\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\r\n\u003c!--  This is a comment  --\u003e\r\n\u003cmain atExample=\"atValue\"\u003e\r\n    \u003cbar\u003evalue bar\u003c/bar\u003e\r\n    \u003cfoo\u003evalue foo\u003c/foo\u003e\r\n    \u003cder at1=\"at1val\" at2=\"at2val\"\u003ethis is long text\u003c/der\u003e\r\n\u003c/main\u003e\r\n```\r\n\r\n### Custom root element\r\n\r\n```php\r\n$array = [\r\n    'bar' =\u003e 'value bar',\r\n    'foo' =\u003e 'value foo',\r\n    'der' =\u003e [\r\n        'cdata' =\u003e 'this is long text',\r\n        '@attributes' =\u003e [\r\n            'at1' =\u003e 'at1val',\r\n            'at2' =\u003e 'at2val',\r\n        ],\r\n    ]\r\n];\r\n\r\necho \\darkfriend\\helpers\\Xml::encode(\r\n    $array,\r\n    [\r\n        'root' =\u003e '\u003cresponse/\u003e',\r\n    ]\r\n);\r\n```\r\n\r\n```xml\r\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\r\n\u003cresponse\u003e\r\n    \u003cbar\u003evalue bar\u003c/bar\u003e\r\n    \u003cfoo\u003evalue foo\u003c/foo\u003e\r\n    \u003cder at1=\"at1val\" at2=\"at2val\"\u003e\u003c![CDATA[this is long text]]\u003e\u003c/der\u003e\r\n\u003c/response\u003e\r\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarkfriend%2Fphp7-xml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdarkfriend%2Fphp7-xml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarkfriend%2Fphp7-xml/lists"}