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

Reading XML

An example 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>

The feed uses an XML namespace, but no prefix.

Using FluentDOM\Query

  1. Create a FluentDOM\Query for the XML
  2. Register a prefix for the Atom namespace
  3. Find all title elements and iterate them.
  4. Output the title element node (will be cast to string)
  5. Output href attribute of the sibling atom:link element.
$fd = FluentDOM($xml);
$fd->registerNamespace('atom', 'http://www.w3.org/2005/Atom');

foreach ($fd->find('//atom:entry/atom:title') as $title) {
  echo $title, "\n";
  echo FluentDOM($title)->siblings('self::atom:link')->attr['href'], "\n\n";
}

Output:

Atom-Powered Robots Run Amok
http://example.org/2003/12/13/atom03

Using FluentDOMs extended DOM API

  1. Create a FluentDOM\DOM\Document
  2. Load the feed
  3. Register a prefix for the Atom namespace
  4. Fetch all title elements using FluentDOM\DOM\Document::evaluate() and iterate them.
  5. Output the title element node (will be cast to string)
  6. Fetch the href attribute of the link element as string using FluentDOM\DOM\Element::evaluate() and output it.
$document = new FluentDOM\DOM\Document();
$document->loadXml($xml);
$document->registerNamespace('atom', 'http://www.w3.org/2005/Atom');

foreach ($document->evaluate('//atom:entry/atom:title') as $title) {
  echo $title, "\n";
  echo $title->evaluate('string(parent::*/atom:link/@href)'), "\n\n";
}

Using PHPs DOM API

For reference, here is how you could do the same with the PHP DOM classes.

  1. Create a DOMDocument
  2. Load the feed
  3. Create a DOMXpath
  4. Register a prefix for the Atom namespace
  5. Fetch all title elements using DOMXpath::evaluate() and iterate them.
  6. Output the node value of the title element node
  7. Fetch the href attribute of the link element as string using DOMXpath::evaluate() providing the title element as context and output it.
// standard DOM
$document = new DOMDocument();
$document->load($feed);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');

foreach ($xpath->evaluate('//atom:entry/atom:title') as $title) {
  echo $title->nodeValue, "\n";
  echo $xpath->evaluate('string(parent::*/atom:link/@href)', $title), "\n\n";
}
Clone this wiki locally