An open API service indexing awesome lists of open source software.

https://github.com/subjective-php/dom

Collection of DOMDocument utilities
https://github.com/subjective-php/dom

Last synced: 9 months ago
JSON representation

Collection of DOMDocument utilities

Awesome Lists containing this project

README

          

# SubjectivePHP\DOM

[![Latest Stable Version](https://poser.pugx.org/subjective-php/dom/v/stable)](https://packagist.org/packages/subjective-php/dom)
[![Latest Unstable Version](https://poser.pugx.org/subjective-php/dom/v/unstable)](https://packagist.org/packages/subjective-php/dom)
[![License](https://poser.pugx.org/subjective-php/dom/license)](https://packagist.org/packages/subjective-php/dom)

[![Total Downloads](https://poser.pugx.org/subjective-php/dom/downloads)](https://packagist.org/packages/subjective-php/dom)
[![Monthly Downloads](https://poser.pugx.org/subjective-php/dom/d/monthly)](https://packagist.org/packages/subjective-php/dom)
[![Daily Downloads](https://poser.pugx.org/subjective-php/dom/d/daily)](https://packagist.org/packages/subjective-php/dom)

[![Documentation](https://img.shields.io/badge/reference-phpdoc-blue.svg?style=flat)](http://www.pholiophp.org/subjective-php/dom)

A collection of utility classes to work with PHP DOM Objects

## Requirements

subjective-php\dom requires PHP 7.0 (or later).

##Composer
To add the library as a local, per-project dependency use [Composer](http://getcomposer.org)! Simply add a dependency on
`subjective-php/dom` to your project's `composer.json` file such as:

```sh
composer require subjective-php/dom
```
##Contact
Developers may be contacted at:

* [Pull Requests](https://github.com/subjective-php/dom/pulls)
* [Issues](https://github.com/subjective-php/dom/issues)

##Run Unit Tests
With a checkout of the code get [Composer](http://getcomposer.org) in your PATH and run:

```sh
composer install
./vendor/bin/phpunit
```
# Examples

### Convert an xml document to an array
```php


Gambardella, Matthew
XML Developer's Guide
Computer
44.95
2000-10-01
An in-depth look at creating applications with XML.


Ralls, Kim
Midnight Rain
Fantasy
5.95
2000-12-16
A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.

XML;

$document = new \DOMDocument();
$document->loadXml($xml);
$array = Util\DOMDocument::toArray($document);

var_export($array);

```

Output will be similar to:

```
array (
'catalog' =>
array (
'book' =>
array (
0 =>
array (
'@id' => 'bk101',
'author' => 'Gambardella, Matthew',
'title' => 'XML Developer\'s Guide',
'genre' => 'Computer',
'price' => '44.95',
'publish_date' => '2000-10-01',
'description' => 'An in-depth look at creating applications with XML.',
),
1 =>
array (
'@id' => 'bk102',
'author' => 'Ralls, Kim',
'title' => 'Midnight Rain',
'genre' => 'Fantasy',
'price' => '5.95',
'publish_date' => '2000-12-16',
'description' => 'A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.',
),
),
),
)
```

### Convert an array to XML
```php
[
[
'@id' => '58339e95d52d9',
'author' => 'Corets, Eva',
'title' => 'The Sundered Grail',
'genre' => 'Fantasy',
'price' => 5.95,
'published' => 1000094400,
'description' => "The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.",
],
[
'@id' => '58339e95d530e',
'author' => 'Randall, Cynthia',
'title' => 'Lover Birds',
'genre' => 'Romance',
'price' => 4.95,
'published' => 967867200,
'description' => 'When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.',
],
],
];

$document = Util\DOMDocument::fromArray(['catalog' => $catalog]);
$document->formatOutput = true;
echo $document->saveXml();
```
#### Output

```


Corets, Eva
The Sundered Grail
Fantasy
5.95
1000094400
The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.


Randall, Cynthia
Lover Birds
Romance
4.95
967867200
When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.

```

### Construct XML document using xpaths

```php
formatOutput = true;
Util\DOMDocument::addXPath($document, "/catalog/book[@id='58339e95d530e']/title", 'Lover Birds');
Util\DOMDocument::addXPath($document, '/catalog/book[@id="58339e95d530e"]/price', 4.95);
Util\DOMDocument::addXPath($document, '/catalog/book[@id="58339e95d52d9"]/title', 'The Sundered Grail');
Util\DOMDocument::addXPath($document, '/catalog/book[@id="58339e95d52d9"]/genre', 'Fantasy');
echo $document->saveXml();
```
#### Output
```


Lover Birds
4.95


The Sundered Grail
Fantasy

```