1. Go to this page and download the library: Download sbwerewolf/xml-navigator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$xml = <<<XML
<doc attrib="a" option="o" >
<base/>
<valuable>element value</valuable>
<complex>
<a empty=""/>
<b val="x"/>
<b val="y"/>
<b val="z"/>
<c>0</c>
<c v="o"/>
<c/>
<different/>
</complex>
</doc>
XML;
$content = \SbWereWolf\XmlNavigator\Convertation\FastXmlToArray
::convert($xml);
$navigator =
new \SbWereWolf\XmlNavigator\Navigation\XmlElement($content);
/* get name of element */
echo $navigator->name() . PHP_EOL;
/* doc */
/* get value of element */
echo "`{$navigator->value()}`" . PHP_EOL;
/* `` */
/* get list of attributes */
$attributes = $navigator->attributes();
foreach ($attributes as $attribute) {
/** @var \SbWereWolf\XmlNavigator\Navigation\IXmlAttribute $attribute */
echo "`{$attribute->name()}` `{$attribute->value()}`" . PHP_EOL;
}
/*
`attrib` `a`
`option` `o`
*/
/* get value of attribute */
echo $navigator->get('attrib') . PHP_EOL;
/* a */
/* get list of nested elements */
$elements = $navigator->elements();
foreach ($elements as $element) {
echo "{$element->name()}" . PHP_EOL;
}
/*
base
valuable
complex
*/
/* get desired nested element */
/** @var \SbWereWolf\XmlNavigator\Navigation\IXmlElement $elem */
$elem = $navigator->pull('valuable')->current();
echo $elem->name() . PHP_EOL;
/* valuable */
/* get all nested elements */
foreach ($navigator->pull() as $pulled) {
/** @var \SbWereWolf\XmlNavigator\Navigation\IXmlElement $pulled */
echo $pulled->name() . PHP_EOL;
/*
base
valuable
complex
*/
}
/* get nested element with given name */
/** @var \SbWereWolf\XmlNavigator\Navigation\IXmlElement $nested */
$nested = $navigator->pull('complex')->current();
/* get names of all elements of nested element */
$elements = $nested->elements();
foreach ($elements as $element) {
echo "{$element->name()}" . PHP_EOL;
}
/*
a
b
b
b
c
c
c
different
*/
/* pull all elements with name `b` */
foreach ($nested->pull('b') as $b) {
/** @var \SbWereWolf\XmlNavigator\Navigation\IXmlElement $b */
echo ' element with name' .
' `' . $b->name() .
'` have attribute `val` with value' .
' `' . $b->get('val') . '`' .
PHP_EOL;
}
/*
element with name `b` have attribute `val` with value `x`
element with name `b` have attribute `val` with value `y`
element with name `b` have attribute `val` with value `z`
*/
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.