Skip to content
Thomas Weinert edited this page Jul 12, 2018 · 2 revisions

Read XML

The main difference between reading XML and HTML are namespaces. So lets read some Atom feed.

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

This example feed does not use a namespace prefix, but that does not make a difference for reading it. FluentDOM allows you to register you own prefixes on the FluentDOM\DOM\Document object. These will be used in xpath expressions and DOM methods.

$document = FluentDOM::load($xmlString);
$document->registerNamespace('a', 'http://www.w3.org/2005/Atom');

foreach ($document('/a:feed/a:entry') as $entry) {
  echo $entry('string(a:title)'), "\n\n";
  echo $entry('string(a:summary)'), "\n\n";
}

Output:

Atom-Powered Robots Run Amok

Some text.

Registering the namespace is important, without that the Xpath expression will not be able to resolve the namespaces. FluentDOM does not automatically register the context namespace prefixes.

Using an extended node as a function is a shortcut to DOMXpath::evaluate(). So it can be used to fetch a node list or a scalar values. It only depends on the Xpath expression.

Clone this wiki locally