aircury / xml
XML manipulation with PHP made easy
Installs: 571
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/aircury/xml
Requires
- aircury/collection: dev-master
Requires (Dev)
- phpunit/phpunit: ^8.2
This package is auto-updated.
Last update: 2025-10-17 19:19:55 UTC
README
XML
XML manipulation with PHP made easy
Installation
Download the library
This library makes use of Composer. You need to have it on your machine. See Composer for instructions. Here we are assuming that you have Composer installed globally.
$ composer require aircury/xml
Documentation
Class documentation
Xml
parseString(string $xmlString): NodeGiven an xml string, parses that string into a Node object.parseFile(string $path): NodeGiven a path, parses that file into a Node object.dump(Node $node): stringGiving any Node, will dump it into an xml string.
Node
Node is the base class.
If you want to set some attributes, there are three options:
- You can pass them to the constructor. E.g.
new Node('pizza', ['crust' => 'classic']) - Node implements \ArrayAccess, so you can use
$pizza['crust']for setting or reading the attributes. - Node has a public attribute called
attributes, so you can access them directly.$pizza->attributes['crust'] = 'classic';
NodeCollection
It is a collection of nodes.
Methods
indexByAttribute(string $attribute): NodeCollectionGiven an attribute it will index the collection by itgetNamedChildren(string $childName, array $attributes = [], bool $createIfMissing = false): NodeCollectionGet a subset of the child nodes, filtered by$attributes. If$createIfMissingis passed, it will ensure it is created if there are no matches.getNamedChild(string $childName, array $attributes = [], bool $createIfMissing = true): NodeSame asgetNamedChildrenbut expecting only one match.
Example
$pizza = new Node('pizza'); // Create a new 'pizza' node. $peperoni = new Node('ingredient', ['name' => 'peperoni', 'spicy' => 'true']); $slice = new Node('slice'); $pizza->addChild($peperoni); $pizza->addChild(new Node('ingredient', ['name' => 'cheese', 'type' => 'cheddar'])); $pizza->addChild(new Node('ingredient', ['name' => 'cheese', 'type' => 'camembert'])); $pizza->addChild($slice); $pizza['crust'] = 'classic'; // Set the crust attribute of the pizza node unset($peperoni['spicy']); // Remove an attirbute from the peperoni ingredient node if (isset($peperoni['spicy'])) { // ... } // Get all the 'ingredient' nodes of the 'pizza' $ingredients = $pizza->namedChildren['ingredient']; // Access the peperoni ingredient $pizza->namedChildren['ingredient'][0]; // If you want to access them by any of the attributes, you can index them by that attribute $pizza->indexByAttribute('ingredient', 'name'); $pizza->namedChildren['ingredient']['peperoni']; // If you want to filter the child nodes by an attribute, you can use `getNamedChildren` $cheeses = $pizza->getNamedChildren('ingredient', ['name' => 'cheese']); // Will return a NodeCollection with two elements // By default, if the children that you are after don't exist, it will create them. if you don't want them to be created // pass the third argument as false $emptyCollection = $pizza->getNamedChildren('ingredient', ['name' => 'mushroom'], false); // If there is only one child, you can also use `getNamedChild` $slice = $pizza->getNamedChild('slice');
License
This software is published under the MIT license.