Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Creating XML with Namespaces (Atom)

Thomas Weinert edited this page · 2 revisions
Clone this wiki locally

Creating XML With Namespaces (Atom)

The FluentDOM\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.

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

$feed = $dom->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.');

$dom->formatOutput = TRUE;
echo $dom->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>
Something went wrong with that request. Please try again.