stepandalecky/xml-element

A better XML element that is easy to iterate on and keeps XML structure under control.

v0.2.2 2018-11-03 20:46 UTC

This package is not auto-updated.

Last update: 2024-11-11 06:04:14 UTC


README

Latest Stable Version License

Read XML in a more convenient way.

StepanDalecky/xml-element is:

  • comfortable to use,
  • easy to iterate on,
  • keeping XML structure under control,
  • predictable.

Installation

Using composer:

composer require stepandalecky/xml-element

Usage

<grandpa name="Splinter" species="rat">
	<father name="Donatello">
		<sohn>me</sohn>
	</father>
	<uncle name="Michelangelo"></uncle>
	<uncle name="Leonardo"></uncle>
</grandpa>
use StepanDalecky\XmlElement\Element;

$grandpa = Element::fromString($xmlString);

$grandpa->getAttributes(); // returns ['name' => 'Splinter', 'species' => 'rat']
$grandpa->getAttribute('name'); // returns 'Splinter'

$grandpa->getChild('father')
	->getChild('sohn')
	->getValue(); // returns 'me'
$grandpa->getChild('mother'); // throws an exception - not found
$grandpa->hasChild('mother'); // returns false
$grandpa->getChild('uncle'); // throws an exception - more children found
$grandpa->hasChild('uncle'); // returns true

$grandpa->getChildren('uncle'); // returns an array consisting of two elements