Skip to content

Creating XML with Namespaces (Atom)

Thomas Weinert edited this page Jul 12, 2018 · 4 revisions

Creating XML With Namespaces (Atom)

The FluentDOM\DOM\Document includes a namespace resolver. You can register namespaces that will be used to resolve qualified names in methods like createElement() or setAttribute(). You can even register a default namespace.

$document = new FluentDOM\DOM\Document();
$document->registerNamespace('#default', 'http://www.w3.org/2005/Atom');

$feed = $document->appendElement('feed');
$feed->appendElement('title', 'Example Feed');
$feed->appendElement('link', ['href' => 'http://example.org/']);
$feed->appendElement('updated', '2003-12-13T18:30:02Z');
$feed->appendElement('author')->appendElement('name', 'John Doe');
$feed->appendElement('id', 'urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6');

$entry = $feed->appendElement('entry');
$entry->appendElement('title', 'Atom-Powered Robots Run Amok');
$entry->appendElement('link', ['href' => 'http://example.org/2003/12/13/atom03']);
$entry->appendElement('id', 'urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a');
$entry->appendElement('updated', '2003-12-13T18:30:02Z');
$entry->appendElement('summary', 'Some text.');

$document->formatOutput = TRUE;
echo $document->saveXml();

Output

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Example Feed</title>
  <link href="http://example.org/"/>
  <updated>2003-12-13T18:30:02Z</updated>
  <author>
    <name>John Doe</name>
  </author>
  <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
  <entry>
    <title>Atom-Powered Robots Run Amok</title>
    <link href="http://example.org/2003/12/13/atom03"/>
    <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
    <updated>2003-12-13T18:30:02Z</updated>
    <summary>Some text.</summary>
  </entry>
</feed>
Clone this wiki locally