{"id":28754327,"url":"https://github.com/zeroasterisk/cakephp-arraytoxml-lib","last_synced_at":"2025-06-17T01:09:00.046Z","repository":{"id":1770604,"uuid":"2689622","full_name":"zeroasterisk/CakePHP-ArrayToXml-Lib","owner":"zeroasterisk","description":"CakePHP lib, static methods to convert an ArrayToXml","archived":false,"fork":false,"pushed_at":"2011-11-04T20:47:18.000Z","size":98,"stargazers_count":2,"open_issues_count":1,"forks_count":2,"subscribers_count":0,"default_branch":"master","last_synced_at":"2023-03-11T05:07:59.252Z","etag":null,"topics":[],"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/zeroasterisk.png","metadata":{"files":{"readme":"README.textile","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":"2011-11-01T18:09:32.000Z","updated_at":"2013-12-17T19:51:40.000Z","dependencies_parsed_at":"2022-09-07T17:01:10.077Z","dependency_job_id":null,"html_url":"https://github.com/zeroasterisk/CakePHP-ArrayToXml-Lib","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"purl":"pkg:github/zeroasterisk/CakePHP-ArrayToXml-Lib","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroasterisk%2FCakePHP-ArrayToXml-Lib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroasterisk%2FCakePHP-ArrayToXml-Lib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroasterisk%2FCakePHP-ArrayToXml-Lib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroasterisk%2FCakePHP-ArrayToXml-Lib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zeroasterisk","download_url":"https://codeload.github.com/zeroasterisk/CakePHP-ArrayToXml-Lib/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeroasterisk%2FCakePHP-ArrayToXml-Lib/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260269462,"owners_count":22983647,"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":[],"created_at":"2025-06-17T01:08:25.249Z","updated_at":"2025-06-17T01:09:00.028Z","avatar_url":"https://github.com/zeroasterisk.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Ever need to constuct XML on the fly?  Like maybe with XML APIs... Are all your inputs/values are as asociative arrays?\n\nFinding myself in that boat pretty often, I have created this simple library to convert associative arrays into XML.\n\nYou can do this two ways:\n\n\u003cbig\u003e\u003ccode\u003eArrayToXml::build($array)\u003c/code\u003e\u003c/big\u003e\n* uses CakePHP's XmlHelper to construct, \n* this lets you construct XML segments easier, which you can put together later\n* NOTE: this XML will not have a root element, unless the array does \n\nor if you prefer SimpleXml (which is faster)\n\n\u003cbig\u003e\u003ccode\u003eArrayToXml::simplexml($array, $rootNodeName, $rootNodeAttributes)\u003c/code\u003e\u003c/big\u003e\n* NOTE: this assumes the $array doesn't have a root element\n\nThe \u003ccode\u003e$array\u003c/code\u003e should be structured as you want the XML structured, keys as elements.\n\n\u003cpre\u003e\u003ccode\u003e$array = array(\n  'Parent' =\u003e array(\n\t\t'Child1' =\u003e array('id' =\u003e 1234, 'name' =\u003e 'child 1'),\n\t\t'Child2' =\u003e array('id' =\u003e 9876, 'name' =\u003e 'child 2'),\n\t),\n);\n$xmlStrBuild = str_replace(\"\\n\", \"\", ArrayToXml::build($array));\n$expected = '\u003cParent\u003e\u003cChild1\u003e\u003cid\u003e1234\u003c/id\u003e\u003cname\u003echild 1\u003c/name\u003e\u003c/Child1\u003e\u003cChild2\u003e\u003cid\u003e9876\u003c/id\u003e\u003cname\u003echild 2\u003c/name\u003e\u003c/Child2\u003e\u003c/Parent\u003e';\n$this-\u003eassertEqual($xmlStrBuild, $expected);\n$xmlStrBuild = str_replace(\"\\n\", \"\", ArrayToXml::build($array, true));\n$expected = '\u003c?xml version=\"1.0\" encoding=\"UTF-8\" ?\u003e\u003cParent\u003e\u003cChild1\u003e\u003cid\u003e1234\u003c/id\u003e\u003cname\u003echild 1\u003c/name\u003e\u003c/Child1\u003e\u003cChild2\u003e\u003cid\u003e9876\u003c/id\u003e\u003cname\u003echild 2\u003c/name\u003e\u003c/Child2\u003e\u003c/Parent\u003e';\n$this-\u003eassertEqual($xmlStrBuild, $expected);\n\n$xmlStrSimple = str_replace(\"\\n\", \"\", ArrayToXml::simplexml($array['Parent'], 'Parent'));\n$expected = '\u003c?xml version=\"1.0\"?\u003e\u003cParent\u003e\u003cChild1\u003e\u003cid\u003e1234\u003c/id\u003e\u003cname\u003echild 1\u003c/name\u003e\u003c/Child1\u003e\u003cChild2\u003e\u003cid\u003e9876\u003c/id\u003e\u003cname\u003echild 2\u003c/name\u003e\u003c/Child2\u003e\u003c/Parent\u003e';\n$this-\u003eassertEqual($xmlStrBuild, $expected);\n$xmlStrSimple = str_replace(\"\\n\", \"\", ArrayToXml::simplexml($array, 'Root'));\n$expected = '\u003c?xml version=\"1.0\"?\u003e\u003cRoot\u003e\u003cParent\u003e\u003cChild1\u003e\u003cid\u003e1234\u003c/id\u003e\u003cname\u003echild 1\u003c/name\u003e\u003c/Child1\u003e\u003cChild2\u003e\u003cid\u003e9876\u003c/id\u003e\u003cname\u003echild 2\u003c/name\u003e\u003c/Child2\u003e\u003c/Parent\u003e\u003c/Root\u003e';\n$this-\u003eassertEqual($xmlStrBuild, $expected);\n\u003c/code\u003e\u003c/pre\u003e\n\nIf you want to inject attributes for any element, you can do so in one of two ways:\n\n\u003cpre\u003e\u003ccode\u003e$array = array(\n'Parent' =\u003e array(\n\t'attrib' =\u003e array('attr1' =\u003e 0),\n\t'Child1' =\u003e array('id' =\u003e 1234, 'name' =\u003e 'child 1', 'attrib' =\u003e array('attr1' =\u003e 1)),\n\t'Child2' =\u003e array('id' =\u003e 9876, 'name' =\u003e 'child 2', 'attrib' =\u003e array('attr1' =\u003e 2)),\n\t),\n);\n$xml = str_replace(\"\\n\", \"\", ArrayToXml::build($array));\n$expected = '\u003cParent attr1=\"0\"\u003e\u003cChild1 attr1=\"1\"\u003e\u003cid\u003e1234\u003c/id\u003e\u003cname\u003echild 1\u003c/name\u003e\u003c/Child1\u003e\u003cChild2 attr1=\"2\"\u003e\u003cid\u003e9876\u003c/id\u003e\u003cname\u003echild 2\u003c/name\u003e\u003c/Child2\u003e\u003c/Parent\u003e';\n$this-\u003eassertEqual($xml, $expected);\n\u003c/code\u003e\u003c/pre\u003e\n\nOr you can suffix the keys, separated by a pipe: _(I like this method most of the time)_\n\n\u003cpre\u003e\u003ccode\u003e$array = array(\n\t'Parent|{\"attr1\":\"0\"}' =\u003e array(\n\t\t'Child1|{\"attr1\":\"1\"}' =\u003e array('id' =\u003e 1234, 'name' =\u003e 'child 1'),\n\t\t'Child2|{\"attr1\":\"2\"}' =\u003e array('id' =\u003e 9876, 'name' =\u003e 'child 2'),\n\t),\n);\n$xml = str_replace(\"\\n\", \"\", ArrayToXml::build($array));\n$expected = '\u003cParent attr1=\"0\"\u003e\u003cChild1 attr1=\"1\"\u003e\u003cid\u003e1234\u003c/id\u003e\u003cname\u003echild 1\u003c/name\u003e\u003c/Child1\u003e\u003cChild2 attr1=\"2\"\u003e\u003cid\u003e9876\u003c/id\u003e\u003cname\u003echild 2\u003c/name\u003e\u003c/Child2\u003e\u003c/Parent\u003e';\n$this-\u003eassertEqual($xml, $expected);\n\u003c/code\u003e\u003c/pre\u003e\n\nYou can use this same trick to iterate over multiple nodes which should have the same name\n\n\u003cpre\u003e\u003ccode\u003e$array = array(\n\t'Parent' =\u003e array(\n\t\t'Child|1' =\u003e array('id' =\u003e 1234, 'name' =\u003e 'child 1'),\n\t\t'Child|2' =\u003e array('id' =\u003e 9876, 'name' =\u003e 'child 2'),\n\t\t'Child|{\"attr1\":\"with attributes\"}|3' =\u003e array('id' =\u003e 3, 'name' =\u003e 'child 3'),\n\t),\n);\n$xml = str_replace(\"\\n\", \"\", ArrayToXml::build($array));\n$expected = '\u003cParent\u003e\u003cChild\u003e\u003cid\u003e1234\u003c/id\u003e\u003cname\u003echild 1\u003c/name\u003e\u003c/Child\u003e\u003cChild\u003e\u003cid\u003e9876\u003c/id\u003e\u003cname\u003echild 2\u003c/name\u003e\u003c/Child\u003e\u003cChild attr1=\"with attributes\"\u003e\u003cid\u003e3\u003c/id\u003e\u003cname\u003echild 3\u003c/name\u003e\u003c/Child\u003e\u003c/Parent\u003e';\n$this-\u003eassertEqual($xml, $expected);\n\u003c/code\u003e\u003c/pre\u003e\n\nYou can also use this helper function for this:\n\n\u003cpre\u003e\u003ccode\u003e$array = array(\n\t'Parent' =\u003e array(\n\t\tarray('id' =\u003e 1234, 'name' =\u003e 'child 1'),\n\t\tarray('id' =\u003e 9876, 'name' =\u003e 'child 2', 'attrib' =\u003e array('attr1' =\u003e 2)),\n\t),\n);\n$array = ArrayToXml::add_tags($array, 'Child', true);\nprint_r($array); \u003e\u003e\u003e\nArray\n(\n\t[Parent] =\u003e Array\n\t\t(\n\t\t\t[Child|0] =\u003e Array\n\t\t\t\t(\n\t\t\t\t\t[id] =\u003e 1234\n\t\t\t\t\t[name] =\u003e child 1\n\t\t\t\t)\n\n\t\t\t[Child|1] =\u003e Array\n\t\t\t\t(\n\t\t\t\t\t[id] =\u003e 9876\n\t\t\t\t\t[name] =\u003e child 2\n\t\t\t\t\t[attrib] =\u003e Array\n\t\t\t\t\t\t(\n\t\t\t\t\t\t\t[attr1] =\u003e 2\n\t\t\t\t\t\t)\n\n\t\t\t\t)\n\n\t\t)\n\n)\n\u003c/code\u003e\u003c/pre\u003e","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeroasterisk%2Fcakephp-arraytoxml-lib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzeroasterisk%2Fcakephp-arraytoxml-lib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeroasterisk%2Fcakephp-arraytoxml-lib/lists"}