PHPackages                             arc/xml - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. arc/xml

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

arc/xml
=======

Ariadne Component Library: xml writer and parser Component

3.0(6y ago)2754542MITPHPPHP &gt;=7.1

Since Jun 20Pushed 5y ago4 watchersCompare

[ Source](https://github.com/Ariadne-CMS/arc-xml)[ Packagist](https://packagist.org/packages/arc/xml)[ Docs](https://github.com/Ariadne-CMS/arc/wiki)[ RSS](/packages/arc-xml/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (2)Versions (12)Used By (2)

ARC: Ariadne Component Library
==============================

[](#arc-ariadne-component-library)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/1ac04bbe583b4dba818a5c8e08cfc6e2ab1baa9feb2378da41286de8e606d733/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f41726961646e652d434d532f6172632d786d6c2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Ariadne-CMS/arc-xml/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/ef0dbabb6f9826fe2178aa1cbaa3d998de85be231077bfccb6625d0e2bb42bb5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f41726961646e652d434d532f6172632d786d6c2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Ariadne-CMS/arc-xml/)[![Latest Stable Version](https://camo.githubusercontent.com/f091272a750ca61c2371fdfedaedab20b5c17b4475da6b17a56cd894c134bedb/68747470733a2f2f706f7365722e707567782e6f72672f6172632f786d6c2f762f737461626c652e737667)](https://packagist.org/packages/arc/xml)[![Total Downloads](https://camo.githubusercontent.com/61768aa0f6e7100b04b6f617c9ec1adef9714dc43ff2ed245b30fb512c974666/68747470733a2f2f706f7365722e707567782e6f72672f6172632f786d6c2f646f776e6c6f6164732e737667)](https://packagist.org/packages/arc/xml)[![Latest Unstable Version](https://camo.githubusercontent.com/874aeb38abd88c5147545a2e9533e28acb5e7215e8fdb55ba0760be3f35d75f5/68747470733a2f2f706f7365722e707567782e6f72672f6172632f786d6c2f762f756e737461626c652e737667)](https://packagist.org/packages/arc/xml)[![License](https://camo.githubusercontent.com/5647ffa516ecb95f2af4f4cf361303d4a927018f8e6990d9184306f4f8b8829c/68747470733a2f2f706f7365722e707567782e6f72672f6172632f786d6c2f6c6963656e73652e737667)](https://packagist.org/packages/arc/xml)

A flexible component library for PHP
------------------------------------

[](#a-flexible-component-library-for-php)

The Ariadne Component Library is a spinoff from the Ariadne Web Application Framework and Content Management System \[  \]

arc/xml
=======

[](#arcxml)

This component provides a unified xml parser and writer. The writer allows for readable and always correct xml in code, without using templates. The parser is a wrapper around both DOMDocument and SimpleXML.

The parser and writer also work on fragments of XML. The parser also makes sure that the output is identical to the input. When converting a node to a string, \\arc\\xml will return the full xml string, including tags. If you don't want that, you can always access the 'nodeValue' property to get the original SimpleXMLElement.

Finally the parser also adds the ability to use basic CSS selectors to find elements in the XML.

Example code:

```
    use \arc\xml as x;
    $xmlString =
        x::preamble()
        .x::rss(['version'=>'2.0'],
             x::channel(
                 x::title('Wikipedia'),
                 x::link('http://www.wikipedia.org'),
                 x::description('This feed notifies you of new articles on Wikipedia.')
             )
        );
```

And parsing it:

```
    $xml = \arc\xml::parse($xmlString);
    $title = $xml->channel->title->nodeValue; // SimpleXMLElement 'Wikipedia'
    $titleTag = $xml->channel->title; // Wikipedia
    echo $title;
```

Installation
------------

[](#installation)

This library requires PHP 7.1 or higher. It is installable and autoloadable via Composer as arc/xml.

```
    composer require arc/xml

```

Parsing XML
===========

[](#parsing-xml)

Examples
--------

[](#examples)

For these examples we'll use the following XML

```

        Slashdot
        http://slashdot.org/
        News for nerds, stuff that matters
        en-us
        2016-01-30T20:38:08+00:00

        Drone Races To Be Broadcast To VR Headsets
        http://hardware.slashdot.org/story/1757209/

        FTDI Driver Breaks Hardware Again
        http://it.slashdot.org/story/1720259/

```

### Getting the title

[](#getting-the-title)

```
    $xml = \arc\xml::parse( $xmlString );
    $title = $xml->channel->title;
    echo $title;
```

result:

```
Slashdot

```

The parser returns the full XML element by default. If you just want the contents, you must be explicit:

```
    $title = $xml->channel->title->nodeValue;
    echo $title;
```

result:

```
Slashdot

```

Instead of the default in SimpleXML, arc\\xml must be explicitly told to get the value of the node using the `nodeValue` property.

### Setting the title

[](#setting-the-title)

```
    $xml->channel->title = 'Update title';
```

As you can see, there is no need to mention the nodeValue here, the name 'title' is enough to select the correct element. It would not make sense to turn the title into another tag entirely here. You can still use the `nodeValue` if you prefer though.

### Getting attributes

[](#getting-attributes)

```
    $about = $xml->channel['rdf:about'];
```

result

```
http://slashdot.org/

```

Just what you would expect, even though there is a namespace in there. When you use a namespace that the parser hasn't been told about before, it will simply look it up in the document and re-use it.

Since attributes aren't XML nodes, there is no nodeValue. Attributes are always returned as just a string.

### Setting attributes

[](#setting-attributes)

```
    $xml->channel['title-attribute'] = 'This is a title attribute';
```

This adds the `title-attribute` if it wasn't there before, or updates it if it was.

### Removing attributes

[](#removing-attributes)

```
   unset($xml->channel['title-attribute']);
```

### Searching the document

[](#searching-the-document)

```
    $items = $xml->find('item');
    echo implode($items);
```

result:

```

        Drone Races To Be Broadcast To VR Headsets
        http://hardware.slashdot.org/story/1757209/

        FTDI Driver Breaks Hardware Again
        http://it.slashdot.org/story/1720259/

```

Again, you get the full XML of the result and it is just an array. (Its been joined here using `implode` for clarity).

The `find()` method accepts most CSS2.0 selectors. For now you can't enter more than one selector, so you can't select 'item, channel' for instance. Either use the SimpleXML `xpath()` method or run multiple queries.

### Searching using namespaces

[](#searching-using-namespaces)

```
    $xml->registerNamespace('dublincore','http://purl.org/dc/elements/1.1/');
    $date = current($xml->find('dublincore|date));
    echo $date;
```

result:

```
    2016-01-30T20:38:08+00:00
```

Again, you get the full XML by default. But in addition, though you've used a namespace alias not known in the document ( `dublincore` ), `find()` returns the `` element for you. The alias is different, but the namespace is the same and that is what matters.

The find() method always returns an array, which may be empty. By using current() you get the first element found, or null if nothing was found.

### Supported CSS Selectors

[](#supported-css-selectors)

The following CSS selectors are supported:

- `tag1 tag2`
    This matches `tag2` which is a descendant of `tag1`.
- `tag1 > tag2`
    This matches `tag2` which is a direct child of `tag1`.
- `tag:first-child`
    This matches `tag` only if its the first child.
- `tag1 + tag2`
    This matches `tag2` only if its immediately preceded by `tag1`.
- `tag1 ~ tag2`
    This matches `tag2` only if it has a previous sibling `tag1`.
- `tag[attr]`
    This matches `tag` if it has the attribute `attr`.
- `tag[attr="foo"]`
    This matches `tag` if it has the attribute `attr` with the value `foo` in its value list.
- `tag#id`
    This matches any `tag` with id `id`.
- `#id`
    This matches any element with id `id`.
- `ns|tag`
    This matches `ns:tag` or more generally `tag` in the namespace indicated by the alias `ns`

SimpleXML
---------

[](#simplexml)

The parsed XML behaves almost identical to a SimpleXMLElement, with the exceptions noted above. So you can access attributes just like SimpleXMLElement allows:

```
    $version = $xml['version'];
    $version = $xml->attributes('version');
```

You can walk through the node tree:

```
    $title = $xml->channel->title;
```

Any method or property available in SimpleXMLElement is included in \\arc\\xml parsed data.

### DOMElement methods

[](#domelement-methods)

In addition to SimpleXMLElement methods, you can also call any method that is available in DOMElement.

```
    $version = $xml->getAttributes('version');
    $title = $xml->getElementsByTagName('channel')[0]
        ->getElementsByTagName('title')[0];
```

### Parsing fragments

[](#parsing-fragments)

The arc\\xml parser accepts partial XML content. It doesn't require a single root element.

```
    $xmlString =
