{"id":19400797,"url":"https://github.com/subjective-php/dom","last_synced_at":"2025-10-11T01:09:01.597Z","repository":{"id":17109266,"uuid":"19875049","full_name":"subjective-php/dom","owner":"subjective-php","description":"Collection of DOMDocument utilities","archived":false,"fork":false,"pushed_at":"2023-07-27T14:42:10.000Z","size":86,"stargazers_count":2,"open_issues_count":1,"forks_count":3,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-13T22:21:54.406Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/subjective-php.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-05-17T00:55:52.000Z","updated_at":"2024-05-23T02:43:39.000Z","dependencies_parsed_at":"2022-07-13T13:51:43.192Z","dependency_job_id":null,"html_url":"https://github.com/subjective-php/dom","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/subjective-php%2Fdom","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/subjective-php%2Fdom/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/subjective-php%2Fdom/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/subjective-php%2Fdom/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/subjective-php","download_url":"https://codeload.github.com/subjective-php/dom/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250579019,"owners_count":21453403,"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":"2024-11-10T11:15:58.864Z","updated_at":"2025-10-11T01:08:56.526Z","avatar_url":"https://github.com/subjective-php.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SubjectivePHP\\DOM\n\n[![Latest Stable Version](https://poser.pugx.org/subjective-php/dom/v/stable)](https://packagist.org/packages/subjective-php/dom)\n[![Latest Unstable Version](https://poser.pugx.org/subjective-php/dom/v/unstable)](https://packagist.org/packages/subjective-php/dom)\n[![License](https://poser.pugx.org/subjective-php/dom/license)](https://packagist.org/packages/subjective-php/dom)\n\n[![Total Downloads](https://poser.pugx.org/subjective-php/dom/downloads)](https://packagist.org/packages/subjective-php/dom)\n[![Monthly Downloads](https://poser.pugx.org/subjective-php/dom/d/monthly)](https://packagist.org/packages/subjective-php/dom)\n[![Daily Downloads](https://poser.pugx.org/subjective-php/dom/d/daily)](https://packagist.org/packages/subjective-php/dom)\n\n[![Documentation](https://img.shields.io/badge/reference-phpdoc-blue.svg?style=flat)](http://www.pholiophp.org/subjective-php/dom)\n\nA collection of utility classes to work with PHP DOM Objects\n\n## Requirements\n\nsubjective-php\\dom requires PHP 7.0 (or later).\n\n##Composer\nTo add the library as a local, per-project dependency use [Composer](http://getcomposer.org)! Simply add a dependency on\n`subjective-php/dom` to your project's `composer.json` file such as:\n\n```sh\ncomposer require subjective-php/dom\n```\n##Contact\nDevelopers may be contacted at:\n\n * [Pull Requests](https://github.com/subjective-php/dom/pulls)\n * [Issues](https://github.com/subjective-php/dom/issues)\n\n##Run Unit Tests\nWith a checkout of the code get [Composer](http://getcomposer.org) in your PATH and run:\n\n```sh\ncomposer install\n./vendor/bin/phpunit\n```\n# Examples\n\n### Convert an xml document to an array\n```php\n\u003c?php\nuse SubjectivePHP\\Util;\n\n$xml = \u003c\u003c\u003cXML\n\u003c?xml version=\"1.0\"?\u003e\n\u003ccatalog\u003e\n  \u003cbook id=\"bk101\"\u003e\n    \u003cauthor\u003eGambardella, Matthew\u003c/author\u003e\n    \u003ctitle\u003eXML Developer's Guide\u003c/title\u003e\n    \u003cgenre\u003eComputer\u003c/genre\u003e\n    \u003cprice\u003e44.95\u003c/price\u003e\n    \u003cpublish_date\u003e2000-10-01\u003c/publish_date\u003e\n    \u003cdescription\u003eAn in-depth look at creating applications with XML.\u003c/description\u003e\n  \u003c/book\u003e\n  \u003cbook id=\"bk102\"\u003e\n    \u003cauthor\u003eRalls, Kim\u003c/author\u003e\n    \u003ctitle\u003eMidnight Rain\u003c/title\u003e\n    \u003cgenre\u003eFantasy\u003c/genre\u003e\n    \u003cprice\u003e5.95\u003c/price\u003e\n    \u003cpublish_date\u003e2000-12-16\u003c/publish_date\u003e\n    \u003cdescription\u003eA former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.\u003c/description\u003e\n  \u003c/book\u003e\nXML;\n\n$document = new \\DOMDocument();\n$document-\u003eloadXml($xml);\n$array = Util\\DOMDocument::toArray($document);\n\nvar_export($array);\n\n```\n\nOutput will be similar to:\n\n```\narray (\n  'catalog' =\u003e\n  array (\n    'book' =\u003e\n    array (\n      0 =\u003e\n      array (\n        '@id' =\u003e 'bk101',\n        'author' =\u003e 'Gambardella, Matthew',\n        'title' =\u003e 'XML Developer\\'s Guide',\n        'genre' =\u003e 'Computer',\n        'price' =\u003e '44.95',\n        'publish_date' =\u003e '2000-10-01',\n        'description' =\u003e 'An in-depth look at creating applications with XML.',\n      ),\n      1 =\u003e\n      array (\n        '@id' =\u003e 'bk102',\n        'author' =\u003e 'Ralls, Kim',\n        'title' =\u003e 'Midnight Rain',\n        'genre' =\u003e 'Fantasy',\n        'price' =\u003e '5.95',\n        'publish_date' =\u003e '2000-12-16',\n        'description' =\u003e 'A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.',\n      ),\n    ),\n  ),\n)\n```\n\n### Convert an array to XML\n```php\n\u003c?php\nuse SubjectivePHP\\Util;\n\n$catalog = [\n    'book' =\u003e [\n        [\n            '@id' =\u003e '58339e95d52d9',\n            'author' =\u003e 'Corets, Eva',\n            'title' =\u003e 'The Sundered Grail',\n            'genre' =\u003e 'Fantasy',\n            'price' =\u003e 5.95,\n            'published' =\u003e 1000094400,\n            'description' =\u003e \"The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.\",\n        ],\n        [\n            '@id' =\u003e '58339e95d530e',\n            'author' =\u003e 'Randall, Cynthia',\n            'title' =\u003e 'Lover Birds',\n            'genre' =\u003e 'Romance',\n            'price' =\u003e 4.95,\n            'published' =\u003e 967867200,\n            'description' =\u003e 'When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.',\n        ],\n    ],\n];\n\n$document = Util\\DOMDocument::fromArray(['catalog' =\u003e $catalog]);\n$document-\u003eformatOutput = true;\necho $document-\u003esaveXml();\n```\n#### Output\n\n```\n\u003c?xml version=\"1.0\"?\u003e\n\u003ccatalog\u003e\n  \u003cbook id=\"58339e95d52d9\"\u003e\n    \u003cauthor\u003eCorets, Eva\u003c/author\u003e\n    \u003ctitle\u003eThe Sundered Grail\u003c/title\u003e\n    \u003cgenre\u003eFantasy\u003c/genre\u003e\n    \u003cprice\u003e5.95\u003c/price\u003e\n    \u003cpublished\u003e1000094400\u003c/published\u003e\n    \u003cdescription\u003eThe two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.\u003c/description\u003e\n  \u003c/book\u003e\n  \u003cbook id=\"58339e95d530e\"\u003e\n    \u003cauthor\u003eRandall, Cynthia\u003c/author\u003e\n    \u003ctitle\u003eLover Birds\u003c/title\u003e\n    \u003cgenre\u003eRomance\u003c/genre\u003e\n    \u003cprice\u003e4.95\u003c/price\u003e\n    \u003cpublished\u003e967867200\u003c/published\u003e\n    \u003cdescription\u003eWhen Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.\u003c/description\u003e\n  \u003c/book\u003e\n\u003c/catalog\u003e\n```\n\n### Construct XML document using xpaths\n\n```php\n\u003c?php\nuse SubjectivePHP\\Util;\n\n$document = new DOMDocument();\n$document-\u003eformatOutput = true;\nUtil\\DOMDocument::addXPath($document, \"/catalog/book[@id='58339e95d530e']/title\", 'Lover Birds');\nUtil\\DOMDocument::addXPath($document, '/catalog/book[@id=\"58339e95d530e\"]/price', 4.95);\nUtil\\DOMDocument::addXPath($document, '/catalog/book[@id=\"58339e95d52d9\"]/title', 'The Sundered Grail');\nUtil\\DOMDocument::addXPath($document, '/catalog/book[@id=\"58339e95d52d9\"]/genre', 'Fantasy');\necho $document-\u003esaveXml();\n```\n#### Output\n```\n\u003c?xml version=\"1.0\"?\u003e\n\u003ccatalog\u003e\n  \u003cbook id=\"58339e95d530e\"\u003e\n    \u003ctitle\u003eLover Birds\u003c/title\u003e\n    \u003cprice\u003e4.95\u003c/price\u003e\n  \u003c/book\u003e\n  \u003cbook id=\"58339e95d52d9\"\u003e\n    \u003ctitle\u003eThe Sundered Grail\u003c/title\u003e\n    \u003cgenre\u003eFantasy\u003c/genre\u003e\n  \u003c/book\u003e\n\u003c/catalog\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsubjective-php%2Fdom","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsubjective-php%2Fdom","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsubjective-php%2Fdom/lists"}