danmichaelo/quitesimplexmlelement

QuiteSimpleXMLElement, formerly known as CustomXMLElement, is a simple wrapper around SimpleXmlElement for easier namespace handling

Installs: 1 489

Dependents: 4

Stars: 1

Watchers: 2

Forks: 1

Language: PHP

v0.2.5 2015-05-14 21:32 UTC

README

Build Status Coverage Status Code Quality Latest Stable Version Total Downloads

The QuiteSimpleXMLElement class is a small wrapper around the SimpleXMLElement class. It was formerly known as CustomXMLElement. The main reason for developing the class was to let objects returned by the xpath() method inherit namespaces from the original object.

Taking an example document,

$xml = '<root xmlns:dc="http://purl.org/dc/elements/1.1/">
    <dc:a>
      <dc:b >
        1.9
      </dc:a>
    </dc:b>
  </root>';

Using SimpleXMLElement I found myself having to register namespaces over and over again:

$root = new SimpleXMLElement($xml);
$root->registerXPathNamespace('d', 'http://purl.org/dc/elements/1.1/');
$a = $root->xpath('d:a');
$a[0]->registerXPathNamespace('d', 'http://purl.org/dc/elements/1.1/');
$b = $a[0]->xpath('d:b');
echo trim((string)$b[0]);

QuiteSimpleXMLElement allows for slightly less typing:

$root = new QuiteSimpleXMLElement($xml);
$root->registerXPathNamespace('d', 'http://purl.org/dc/elements/1.1/');
$a = $root->xpath('d:a');
$b = $a->xpath('d:b');
echo trim((string)$b[0]);

A note on the design: I would have preferred to extend the original SimpleXMLElement class, but the constructor is static, which is why I wrote a wrapper instead.

Convenience methods

The library defines some new methods to support less typing and cleaner code.

attr($name)

Returns the value of an attribute as a string

echo $node->attr('id');
text($xpath)

Returns the text content of the node

echo $node->text('d:a/d:b');
first($xpath)

Returns the first node that matches the given path, or null if none.

$node = $node->first('d:a/d:b');
all($xpath)

Returns all nodes that matches the given path, or an empty array if none.

$node = $node->all('d:a/d:b');
has($xpath)

Returns true if the node exists, false if not

if ($node->has('d:a/d:b') {
    …
}
setValue($value)

Sets the value of a node

$node->setValue('Hello world');
count($namespace=null)

Returns the number of child nodes, optionally within a given namespace.

if ($node->count('d') > 0) {
  …
}