{"id":16380220,"url":"https://github.com/josemmo/uxml","last_synced_at":"2025-07-09T17:41:17.740Z","repository":{"id":51044681,"uuid":"300935793","full_name":"josemmo/uxml","owner":"josemmo","description":"Uncomplicated XML manipulation library for PHP","archived":false,"fork":false,"pushed_at":"2024-02-08T16:55:15.000Z","size":69,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-18T17:14:42.784Z","etag":null,"topics":["php","xml","xml-manipulation","xml-parser"],"latest_commit_sha":null,"homepage":"","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/josemmo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-03T17:18:28.000Z","updated_at":"2023-10-19T09:18:41.000Z","dependencies_parsed_at":"2024-10-28T15:24:03.506Z","dependency_job_id":"8a0f2bd0-910b-482f-b3c9-41f9d9f42396","html_url":"https://github.com/josemmo/uxml","commit_stats":{"total_commits":38,"total_committers":1,"mean_commits":38.0,"dds":0.0,"last_synced_commit":"a2bcbf87f0386c1ac46836364a6f3004c592fd7a"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josemmo%2Fuxml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josemmo%2Fuxml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josemmo%2Fuxml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josemmo%2Fuxml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josemmo","download_url":"https://codeload.github.com/josemmo/uxml/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245052658,"owners_count":20553162,"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":["php","xml","xml-manipulation","xml-parser"],"created_at":"2024-10-11T03:50:50.976Z","updated_at":"2025-03-23T03:33:10.944Z","avatar_url":"https://github.com/josemmo.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Uncomplicated XML\n[![Build Status](https://github.com/josemmo/uxml/workflows/CI/badge.svg)](https://github.com/josemmo/uxml/actions)\n[![Latest Version](https://img.shields.io/packagist/v/josemmo/uxml)](https://packagist.org/packages/josemmo/uxml)\n[![Minimum PHP Version](https://img.shields.io/packagist/php-v/josemmo/uxml)](#installation)\n[![License](https://img.shields.io/github/license/josemmo/uxml)](LICENSE)\n\nUXML is an *extremely* simple PHP library for manipulating XML documents with ease while keeping overhead to a bare minimum.\n\nIt consist of just a single class which uses the PHP built-in `DOMElement` and `DOMDocument` classes under the hood.\n\n## Installation\n\n### Using Composer\n```\ncomposer require josemmo/uxml\n```\n\n### Without Composer\nDownload source files from the GitHub repository:\n```\ngit clone https://github.com/josemmo/uxml.git\n```\n\nUse the UXML class in your app:\n```php\nuse UXML\\UXML;\nrequire_once __DIR__ . \"/uxml/src/UXML.php\";\n```\n\n## FAQ\n**Why use this instead of [sabre/xml](https://github.com/sabre-io/xml) or [FluidXML](https://github.com/servo-php/fluidxml)?**\\\nBoth those options are great and if they fit your project you should definetely use them! However, in my case I needed something more lightweight to put on top of [LibXML's DOM](https://www.php.net/manual/en/book.dom.php) to provide an alternative syntax.\n\n**Is UXML compatible with `DOMElement`?**\\\nYes, indeed! You can get the original `DOMElement` instance from an `UXML` object and vice versa.\n\n**I want UXML to do \"X\". Can you implement it?**\\\nMy main goal with this project is not to implement all possible behaviors in the XML specification, you can use the DOM or SimpleXML libraries for that.\n\n## Usage\n\n### Create a new document\nUXML does not distinguish between XML documents (`DOMDocument`) and elements (`DOMElement`). Instead, you can create a new document like so:\n```php\n$xml = \\UXML\\UXML::newInstance('RootTagName');\n```\n\nYou can also wrap an already existing `DOMElement`:\n```php\n$domElement = new DOMElement('TagName');\n$xml = \\UXML\\UXML::fromElement($domElement);\n```\n\n### Load an XML document from source\nBy loading an XML string, UXML will return the root element of the document tree:\n```php\n$source = \u003c\u003c\u003cXML\n\u003cfruits\u003e\n    \u003cfruit\u003eBanana\u003c/fruit\u003e\n    \u003cfruit\u003eApple\u003c/fruit\u003e\n    \u003cfruit\u003eTomato\u003c/fruit\u003e\n\u003c/fruits\u003e\nXML;\n$xml = \\UXML\\UXML::fromString($source);\n```\n\n### Add elements to a node\nWhen adding an element, UXML will return a reference to the newly created element:\n```php\n$xml = \\UXML\\UXML::newInstance('Parent');\n$child = $xml-\u003eadd('Child');\necho $child; // \u003cChild /\u003e\necho $xml;   // \u003cParent\u003e\u003cChild /\u003e\u003c/Parent\u003e\n```\n\nYou can also define a value:\n```php\n$child = $xml-\u003eadd('Child', 'Hello World!');\necho $child; // \u003cChild\u003eHello World!\u003c/Child\u003e\n```\n\nAnd even attributes or namespaces:\n```php\n$feed = \\UXML\\UXML::newInstance('feed', null, [\n    'xmlns' =\u003e 'http://www.w3.org/2005/Atom'\n]);\necho $feed; // \u003cfeed xmlns=\"http://www.w3.org/2005/Atom\" /\u003e\n\n$link = $feed-\u003eadd('link', 'Wow!', [\n    'href' =\u003e 'https://www.example.com'\n]);\necho $link; // \u003clink href=\"https://www.example.com\"\u003eWow!\u003c/link\u003e\n```\n\n### Element chaining\nBecause with every element insertion a reference to the new element is returned, you can chain multiple of these calls to create a tree:\n```php\n$xml = \\UXML\\UXML::newInstance('people');\n$xml-\u003eadd('person')-\u003eadd('name', 'Jane Doe');\necho $xml; // \u003cpeople\u003e\n           //     \u003cperson\u003e\n           //         \u003cname\u003eJane Doe\u003c/name\u003e\n           //     \u003c/person\u003e\n           // \u003c/people\u003e\n```\n\n### Export XML source\nBesides casting `UXML` objects to a `string`, there is a method for exporting the XML source of an element and its children:\n```php\n$xml-\u003easXML();\n```\n\nBy default, exported strings include an XML declaration (except when casting `UXML` instances to a `string`).\n\n### Find XML elements\nUXML allows you to use XPath 1.0 queries to get a particular element from a document:\n```php\n$xml = \\UXML\\UXML::newInstance('person');\n$xml-\u003eadd('name', 'Jane');\n$xml-\u003eadd('surname', 'Doe');\n$xml-\u003eadd('color', 'green', ['hex' =\u003e '#0f0']);\n\necho $xml-\u003eget('*[@hex]'); // \u003ccolor hex=\"#0f0\"\u003egreen\u003c/color\u003e\nvar_dump($xml-\u003eget('birthday')); // NULL\n```\n\nOr even multiple elements:\n```php\n$xml = \\UXML\\UXML::fromString('\u003ca\u003e\u003cb\u003e1\u003c/b\u003e\u003cb\u003e2\u003c/b\u003e\u003cb\u003e3\u003c/b\u003e\u003c/a\u003e');\nforeach ($xml-\u003egetAll('b') as $elem) {\n    echo \"Element says: \" . $elem-\u003easText() . \"\\n\";\n}\n```\n\nNote all XPath queries are **relative to current element**:\n```php\n$source = \u003c\u003c\u003cXML\n\u003cmovie\u003e\n    \u003cname\u003eInception\u003c/name\u003e\n    \u003cyear\u003e2010\u003c/year\u003e\n    \u003cdirector\u003e\n        \u003cname\u003eChristopher\u003c/name\u003e\n        \u003csurname\u003eNolan\u003c/surname\u003e\n        \u003cyear\u003e1970\u003c/year\u003e\n    \u003c/director\u003e\n\u003c/movie\u003e\nXML;\n$xml = \\UXML\\UXML::fromString($source);\n\necho $xml-\u003eget('director/year'); // \u003cyear\u003e1970\u003c/year\u003e\necho $xml-\u003eget('director')-\u003eget('year'); // \u003cyear\u003e1970\u003c/year\u003e\necho $xml-\u003eget('year'); // \u003cyear\u003e2010\u003c/year\u003e\necho $xml-\u003eget('director')-\u003eget('//year'); // \u003cyear\u003e2010\u003c/year\u003e\n```\n\n### Remove XML elements\nElements can be removed from the XML tree by calling the `remove()` method on them.\nAfter an element is removed, it becomes unusable:\n```php\n$source = \u003c\u003c\u003cXML\n\u003cproject\u003e\n    \u003cpublic\u003e\n        \u003cname\u003eAlpha\u003c/name\u003e\n    \u003c/public\u003e\n    \u003cconfidential\u003e\n        \u003cbudget\u003e1,000,000 USD\u003c/budget\u003e\n    \u003c/confidential\u003e\n\u003c/project\u003e\nXML;\n$xml = \\UXML\\UXML::fromString($source);\n$xml-\u003eget('confidential')-\u003eremove();\necho $xml; // \u003cproject\u003e\u003cpublic\u003e\u003cname\u003eAlpha\u003c/name\u003e\u003c/public\u003e\u003c/project\u003e\n```\n\n### Namespaces\nNamespaces are assigned in the same way as other attributes:\n```php\n$xml-\u003eadd('TagName', null, [\n    'xmlns' =\u003e 'https://example.com',\n    'xmlns:abc' =\u003e 'urn:abc',\n    'attribute' =\u003e 'value'\n])-\u003eadd('abc:Child', 'Name');\necho $xml; // \u003cTagName xmlns=\"https://example.com\"\n           //  xmlns:abc=\"urn:abc\"\n           //  attribute=\"value\"\u003e\n           //     \u003cabc:Child\u003eName\u003c/abc:Child\u003e\n           // \u003c/TagName\u003e\n```\n\nHowever, when querying elements, the prefix defined in the document may not be the one you are expecting:\n```php\n$xml = \\UXML\\UXML::fromString('\u003ca xmlns:ns=\"urn:abc\"\u003e\u003cns:b /\u003e\u003c/a\u003e');\necho $xml-\u003eget('ns:b'); // \u003cns:b /\u003e\necho $xml-\u003eget('abc:b'); // Is NULL as the prefix does not exist\n```\n\nTo fix this, you can make use of [clark notation](https://sabre.io/xml/clark-notation/) inside the XPath query:\n```php\necho $xml-\u003eget('{urn:abc}b'); // \u003cns:b /\u003e\n```\n\n### Advanced XML manipulation\nFor any other document manipulation outside the scope of this library, you can always interact with the `DOMElement` instance:\n```php\n$xml = \\UXML\\UXML::newInstance('Test');\n$xml-\u003eelement(); // Returns a [DOMElement] object\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosemmo%2Fuxml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosemmo%2Fuxml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosemmo%2Fuxml/lists"}