DOM with Arrays
Learn how to convert HTML and XML structures to Arrays and vice versa.
DOM to Array
It is possible to convert XML or HTML files—or even strings containing such content—into a complex array or a simple array, depending on your needs; for example:
use Inphinit\Dom\Document;
$handle = new Document(Document::XML);
$handle->loadString('<root xmlns:book="https://book-io"><node foo="bar" baz="foobar">contents</node><book:tag>baz</book:tag></root>');
echo "COMPLETE:\n";
print_r($handle->toArray(Document::ARRAY_COMPLETE));
echo "SIMPLE:\n";
print_r($handle->toArray(Document::ARRAY_SIMPLE));
echo "MINIMAL:\n";
print_r($handle->toArray(Document::ARRAY_MINIMAL));
The result with ARRAY_COMPLETE will generate a complex—yet potentially useful—array that includes the attributes and values of namespaced tags:
print_r($handle->toArray(Document::ARRAY_COMPLETE));
output:
Array
(
[root] => Array
(
[node] => Array
(
[@attributes] => Array
(
[foo] => bar
[baz] => foobar
)
[@contents] => contents
)
[book:tag] => baz
[@attributes] => Array
(
[xmlns:book] => https://book.io
)
)
)
The result with ARRAY_SIMPLE will generate a slightly less complex array:
print_r($handle->toArray(Document::ARRAY_SIMPLE));
output:
Array
(
[root] => Array
(
[node] => Array
(
[@attributes] => Array
(
[foo] => bar
[baz] => foobar
)
[@contents] => contents
)
[book:tag] => baz
[@attributes] => Array
(
[xmlns:book] => https://book.io
)
)
)
While Document::ARRAY_MINIMAL will return a very simple array containing only values and tag keys without namespaces. Attributes will not be converted either:
print_r($handle->toArray(Document::ARRAY_MINIMAL));
output:
Array
(
[root] => Array
(
[node] => contents
)
)
Array to HTML
An array can be converted into HTML or XML; to define the elements, the array keys must be prefixed with @, with the exception of @contents, which is used to populate the value of the generated element. HTML example:
use Inphinit\Dom\Document;
$handle = new Document(Document::HTML);
$handle->fromArray([
'html' => [
'head' => [
'@contents' => 'contents <title>test</title>',
],
'body' => [
'main' => [
'p' => [
'@contents' => 'contents <s>test</s>',
'span' => [
'foo',
'bar',
'baz'
]
],
'div' => [
'foo',
'bar',
'baz'
],
'@comment' => 'test'
],
'@attributes' => [
'data-foo' => 'bar',
'data-baz' => 'foobar'
]
],
'@attributes' => [
'class' => 'sample',
'id' => 'test'
]
]
]);
$handle->dump($handle->root());
Array to XML
An array can be converted to XML in the same way as in the HTML example. To define attributes, the array keys must be prefixed with @, with the exception of @contents, which is used to populate the value of the generated element. HTML example:
use Inphinit\Dom\Document;
$handle = new Document(Document::XML);
$handle->fromArray([
'root' => [
'node' => [
'@attributes' => [
'foo' => 'bar',
'baz' => 'foobar'
],
'foo' => [
'bar',
'baz'
],
'@contents' => 'contents <s>test</s>',
],
'book:tag' => 'baz',
'@attributes' => [
'class' => 'sample',
'xmlns:book' => 'https://book.io'
],
'@comment' => 'foobar'
]
]);
$handle->dump($handle->root());