2017 © Pedro Peláez
 

library dom

Collection of utility classes to work with DOMDocument

image

subjective-php/dom

Collection of utility classes to work with DOMDocument

  • Wednesday, June 6, 2018
  • by chadicus
  • Repository
  • 1 Watchers
  • 1 Stars
  • 185 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 2213 % Grown

The README.md

SubjectivePHP\DOM

Latest Stable Version Latest Unstable Version License, (*1)

Total Downloads Monthly Downloads Daily Downloads, (*2)

Documentation, (*3)

A collection of utility classes to work with PHP DOM Objects, (*4)

Requirements

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

Composer

To add the library as a local, per-project dependency use Composer! Simply add a dependency on subjective-php/dom to your project's composer.json file such as:, (*6)

composer require subjective-php/dom

Contact

Developers may be contacted at:, (*7)

Run Unit Tests

With a checkout of the code get Composer in your PATH and run:, (*8)

composer install
./vendor/bin/phpunit

Examples

Convert an xml document to an array


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

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

var_export($array);

Output will be similar to:, (*9)

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

 [
        [
            '@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

```

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

Construct XML document using xpaths

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
```

<catalog>
  <book id="58339e95d530e">
    <title>Lover Birds</title>
    <price>4.95</price>
  </book>
  <book id="58339e95d52d9">
    <title>The Sundered Grail</title>
    <genre>Fantasy</genre>
  </book>
</catalog>

The Versions

06/06 2018

dev-master

9999999-dev

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-dom *

 

The Development Requires

xml dom domdocument

06/06 2018

v3.1.1

3.1.1.0

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-dom *

 

The Development Requires

xml dom domdocument

15/05 2018

v3.1.0

3.1.0.0

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-dom *

 

The Development Requires

xml dom domdocument

15/03 2018

v3.0.0

3.0.0.0

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-dom *

 

The Development Requires

xml dom domdocument

19/01 2017

v2.1.1

2.1.1.0

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ~5.6 || ~7.0
  • ext-dom *

 

The Development Requires

xml dom domdocument

29/11 2016

v2.1.0

2.1.0.0

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ~5.6 || ~7.0
  • ext-dom *

 

The Development Requires

xml dom domdocument

09/08 2016

v2.0.0

2.0.0.0

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ~5.6 || ~7.0
  • ext-dom *

 

The Development Requires

xml dom domdocument

20/09 2015

v1.0.0

1.0.0.0

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ~5.4 || ~7.0
  • ext-dom *

 

The Development Requires

xml dom domdocument

02/06 2015

v0.4.0

0.4.0.0

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ~5.4 || ~7.0
  • ext-dom *

 

The Development Requires

xml dom domdocument

01/06 2015

v0.3.0

0.3.0.0

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ~5.4 || ~7.0
  • ext-dom *

 

The Development Requires

xml dom domdocument

22/01 2015

v0.2.1

0.2.1.0

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ~5.4
  • ext-dom *

 

The Development Requires

xml dom domdocument

22/01 2015

v0.2.0

0.2.0.0

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ~5.4
  • ext-dom *

 

The Development Requires

xml dom domdocument

21/01 2015

v0.1.2

0.1.2.0

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ~5.4
  • ext-dom *

 

The Development Requires

xml dom domdocument

21/01 2015

v0.1.1

0.1.1.0

Collection of utility classes to work with DOMDocument

  Sources   Download

MIT

The Requires

  • php ~5.4
  • ext-dom *

 

The Development Requires

xml dom domdocument