# fluent-booking/1.5.21/vendor/wpfluent/caldav/src/ICal/XmlParser.php

Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution, version 1.5.21. 67 lines.

- Page: https://pluginprobe.com/plugins/fluent-booking/1.5.21/code/vendor/wpfluent/caldav/src/ICal/XmlParser.php
- Raw: https://pluginprobe.com/plugins/fluent-booking/1.5.21/raw/vendor/wpfluent/caldav/src/ICal/XmlParser.php
- Modified: 2024-07-16T14:25:14+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/fluent-booking/1.5.21/code/vendor/wpfluent/caldav/src/ICal/XmlParser.php#L10-L20`.

```php
<?php

namespace FluentBooking\Package\CalDav\ICal;

use DOMDocument;
use DOMXPath;

class XmlParser
{
	public static function parse($xml)
	{
		$instance = new static;
		
		$doc = new DOMDocument();

        $doc->loadXML($xml);

        $xpath = new DOMXPath($doc);

        $xpath->registerNamespace('d', 'DAV:');

        $list = [];

        foreach ($xpath->query('//d:response') as $res) {
            $list[] = $instance->parseNodes($res);
        }

        return $list;
	}

	protected function parseNodes($x)
    {
        $nodes = [];

        foreach ($x->childNodes as $n) {
            if ($this->hasChild($n)) {
                $nodes[$n->localName][] = $this->parseNodes($n);
            } else if ($n->nodeType == XML_ELEMENT_NODE) {
                $nodes[$n->localName][] = $n->nodeValue;

                if ($n->hasAttributes()) {
                    foreach ($n->attributes as $attr) {
                        $nodes[$n->localName][]['attributers'][$attr->nodeName] = $attr->nodeValue;
                    }

                    $nodes[$n->localName] = array_values(array_filter($nodes[$n->localName]));
                }
            }
        }

        return $nodes;
    }

    protected function hasChild($n)
    {
        if ($n->hasChildNodes()) {
            foreach ($n->childNodes as $c) {
                if ($c->nodeType == XML_ELEMENT_NODE) {
                    return true;
                }
            }
        }

        return false;
    }
}

```
