{"id":22547048,"url":"https://github.com/kiwilan/php-xml-reader","last_synced_at":"2025-04-10T00:53:05.206Z","repository":{"id":176593763,"uuid":"655538502","full_name":"kiwilan/php-xml-reader","owner":"kiwilan","description":"PHP package to read XML with nice API.","archived":false,"fork":false,"pushed_at":"2025-02-03T22:17:48.000Z","size":208,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-10T00:52:58.908Z","etag":null,"topics":["php","xml"],"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/kiwilan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","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},"funding":{"github":"kiwilan"}},"created_at":"2023-06-19T05:31:42.000Z","updated_at":"2025-02-03T22:17:45.000Z","dependencies_parsed_at":"2024-04-15T23:42:20.492Z","dependency_job_id":"f12db4e5-0b96-487f-89c7-f7ef57466244","html_url":"https://github.com/kiwilan/php-xml-reader","commit_stats":null,"previous_names":["kiwilan/php-xml-reader"],"tags_count":13,"template":false,"template_full_name":"spatie/package-skeleton-php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiwilan%2Fphp-xml-reader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiwilan%2Fphp-xml-reader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiwilan%2Fphp-xml-reader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kiwilan%2Fphp-xml-reader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kiwilan","download_url":"https://codeload.github.com/kiwilan/php-xml-reader/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137995,"owners_count":21053775,"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"],"created_at":"2024-12-07T15:09:57.300Z","updated_at":"2025-04-10T00:53:05.181Z","avatar_url":"https://github.com/kiwilan.png","language":"PHP","funding_links":["https://github.com/sponsors/kiwilan"],"categories":[],"sub_categories":[],"readme":"# PHP XML Reader\n\n![Banner with cards catalog picture in background and PHP XML Reader title](https://raw.githubusercontent.com/kiwilan/php-xml-reader/main/docs/banner.jpg)\n\n[![php][php-version-src]][php-version-href]\n[![version][version-src]][version-href]\n[![downloads][downloads-src]][downloads-href]\n[![license][license-src]][license-href]\n[![tests][tests-src]][tests-href]\n[![codecov][codecov-src]][codecov-href]\n\nPHP package to read XML with nice API, heavily inspired by [`stackoverflow answer`](https://stackoverflow.com/a/46349713/11008206).\n\n## About\n\nPHP have native functions to read XML files with [`SimpleXML`](https://www.php.net/manual/en/book.simplexml.php), but it's not easy to use and not very flexible. This package offer to read XML files as array, with a simple and flexible API.\n\n## Requirements\n\n-   **PHP version** \u003e= _8.0_\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require kiwilan/php-xml-reader\n```\n\n## Usage\n\nYou can read XML from path or from content.\n\n-   `mapContent`: `boolean` If a key has only `@content` key, return only the value of `@content`. Default: `true`.\n-   `failOnError`: `boolean` Throw exception if XML is invalid. Default: `true`.\n\n```php\nuse Kiwilan\\XmlReader\\XmlReader;\n\n$xml = XmlReader::make('path/to/file.xml', bool $mapContent = true, bool $failOnError = true);\n```\n\n### Methods\n\n|              Method               |               Description                |               Type                |\n| :-------------------------------: | :--------------------------------------: | :-------------------------------: |\n|         `$xml-\u003egetRoot()`         |          Value of root element           |             `?string`             |\n|        `$xml-\u003egetRootNS()`        |        Namespaces of root element        |            `string[]`             |\n|    `$xml-\u003egetRootAttributes()`    |        Attributes of root element        |      `array\u003cstring, mixed\u003e`       |\n| `$xml-\u003egetRootAttributes('key');` | Value of `key` attribute of root element |              `mixed`              |\n|       `$xml-\u003egetVersion();`       |               XML version                |             `?string`             |\n|      `$xml-\u003egetEncoding();`       |               XML encoding               |             `?string`             |\n|       `$xml-\u003eisValidXml();`       |          Check if XML is valid           |              `bool`               |\n|        `$xml-\u003egetPath();`         |             Path of XML file             |             `?string`             |\n|      `$xml-\u003egetFilename();`       |           Filename of XML file           |             `?string`             |\n|      `$xml-\u003egetConverter();`      |  Converter used to convert XML to array  | `\\Kiwilan\\XmlReader\\XmlConverter` |\n| `$xml-\u003esave('path/to/file.xml');` |              Save XML file               |              `bool`               |\n|        `$xml-\u003etoArray();`         |           Convert XML to array           |      `array\u003cstring, mixed\u003e`       |\n|       `$xml-\u003e__toString();`       |          Convert XML to string           |             `string`              |\n\n### Advanced methods\n\n|              Method               |                       Description                       |               Type               |              Options              |\n| :-------------------------------: | :-----------------------------------------------------: | :------------------------------: | :-------------------------------: |\n|       `$xml-\u003egetContents()`       |              XML as multidimensional array              |      `array\u003cstring, mixed\u003e`      |                                   |\n|        `$xml-\u003efind('key')`        | Find key will return first value where that contain key |      `array\u003cstring, mixed\u003e`      | `strict`, `content`, `attributes` |\n|       `$xml-\u003esearch('key')`       |     Search will return all values that contain key      |             `mixed`              |                                   |\n|     `$xml-\u003eextract(...keys)`      |   Extract `metadata` key, if not found return `null`    |             `mixed`              |                                   |\n|  `XmlReader::parseContent(key)`   |                 Parse content of entry                  |             `mixed`              |                                   |\n| `XmlReader::parseAttributes(key)` |                Parse attributes of entry                | `array\u003cstring, mixed\u003e` or `null` |                                   |\n\n### Basic usage\n\nXML as multidimensional array from `root` (safe).\n\n```php\nuse Kiwilan\\XmlReader\\XmlReader;\n\n$xml = XmlReader::make('path/to/file.xml');\n$contents = $xml-\u003egetContents();\n```\n\nBasic usage.\n\n```php\nuse Kiwilan\\XmlReader\\XmlReader;\n\n$xml = XmlReader::make('path/to/file.xml');\n$contents = $xml-\u003egetContents();\n$title = $contents['metadata']['dc:title'] ?? null;\n```\n\n### Find\n\nFind key will return first value where key that contain `title` (safe).\n\n```php\nuse Kiwilan\\XmlReader\\XmlReader;\n\n$xml = XmlReader::make('path/to/file.xml');\n$title = $xml-\u003efind('title', strict: false);\n```\n\nFind key will return first value where key is `dc:title` (safe)\n\n```php\nuse Kiwilan\\XmlReader\\XmlReader;\n\n$xml = XmlReader::make('path/to/file.xml');\n$dcTitle = $xml-\u003efind('dc:title');\n```\n\nFind key will return first value where key that contain `dc:title` and return `@content` (safe)\n\n```php\nuse Kiwilan\\XmlReader\\XmlReader;\n\n$xml = XmlReader::make('path/to/file.xml');\n$dcCreator = $xml-\u003efind('dc:creator', content: true);\n```\n\nFind key will return first value where key contain `dc:creator` and return `@attributes` (safe)\n\n```php\nuse Kiwilan\\XmlReader\\XmlReader;\n\n$xml = XmlReader::make('path/to/file.xml');\n$dcCreator = $xml-\u003efind('dc:creator', attributes: true);\n```\n\n### Search\n\nSearch will return all values that contain `dc` (safe)\n\n```php\nuse Kiwilan\\XmlReader\\XmlReader;\n\n$xml = XmlReader::make('path/to/file.xml');\n$dc = $xml-\u003esearch('dc');\n```\n\n### Extract\n\nExtract `metadata` key, if not found return null (safe)\n\n```php\nuse Kiwilan\\XmlReader\\XmlReader;\n\n$xml = XmlReader::make('path/to/file.xml');\n$rootKey = $xml-\u003eextract('metadata');\n```\n\nExtract `metadata` and `dc:title` keys (safe)\n\n```php\nuse Kiwilan\\XmlReader\\XmlReader;\n\n$xml = XmlReader::make('path/to/file.xml');\n$subSubKey = $xml-\u003eextract(['metadata', 'dc:title']);\n```\n\n### Get content\n\nIf you want to extract only `@content` you could use `parseContent()` method, if you want to extract only `@attributes` you could use `parseAttributes()` method.\n\nExtract `dc:title` key and return `@content` (safe)\n\n```php\nuse Kiwilan\\XmlReader\\XmlReader;\n\n$xml = XmlReader::make('path/to/file.xml');\n$title = $xml-\u003eextract(['metadata', 'dc:title']);\n$title = XmlReader::parseContent($title);\n```\n\nExtract `dc:creator` key and return `@content` (safe)\n\n```php\nuse Kiwilan\\XmlReader\\XmlReader;\n\n$xml = XmlReader::make('path/to/file.xml');\n$creator = $xml-\u003eextract(['metadata', 'dc:creator']);\n$creator = XmlReader::parseContent($creator);\n```\n\nExtract `dc:creator` key and return `@attributes` (safe)\n\n```php\nuse Kiwilan\\XmlReader\\XmlReader;\n\n$xml = XmlReader::make('path/to/file.xml');\n$creatorAttributes = $xml-\u003eextract(['metadata', 'dc:creator']);\n$creatorAttributes = XmlReader::parseAttributes($creatorAttributes);\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Credits\n\n-   [`spatie`](https://github.com/spatie) for `spatie/package-skeleton-php`\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n[\u003cimg src=\"https://user-images.githubusercontent.com/48261459/201463225-0a5a084e-df15-4b11-b1d2-40fafd3555cf.svg\" height=\"120rem\" width=\"100%\" /\u003e](https://github.com/kiwilan)\n\n[version-src]: https://img.shields.io/packagist/v/kiwilan/php-xml-reader.svg?style=flat\u0026colorA=18181B\u0026colorB=777BB4\n[version-href]: https://packagist.org/packages/kiwilan/php-xml-reader\n[php-version-src]: https://img.shields.io/static/v1?style=flat\u0026label=PHP\u0026message=v8.0\u0026color=777BB4\u0026logo=php\u0026logoColor=ffffff\u0026labelColor=18181b\n[php-version-href]: https://www.php.net/\n[downloads-src]: https://img.shields.io/packagist/dt/kiwilan/php-xml-reader.svg?style=flat\u0026colorA=18181B\u0026colorB=777BB4\n[downloads-href]: https://packagist.org/packages/kiwilan/php-xml-reader\n[license-src]: https://img.shields.io/github/license/kiwilan/php-xml-reader.svg?style=flat\u0026colorA=18181B\u0026colorB=777BB4\n[license-href]: https://github.com/kiwilan/php-xml-reader/blob/main/README.md\n[tests-src]: https://img.shields.io/github/actions/workflow/status/kiwilan/php-xml-reader/run-tests.yml?branch=main\u0026label=tests\u0026style=flat\u0026colorA=18181B\n[tests-href]: https://packagist.org/packages/kiwilan/php-xml-reader\n[codecov-src]: https://img.shields.io/codecov/c/gh/kiwilan/php-xml-reader/main?style=flat\u0026colorA=18181B\u0026colorB=777BB4\n[codecov-href]: https://codecov.io/gh/kiwilan/php-xml-reader\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkiwilan%2Fphp-xml-reader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkiwilan%2Fphp-xml-reader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkiwilan%2Fphp-xml-reader/lists"}