-
Notifications
You must be signed in to change notification settings - Fork 20
Reading XML
Thomas Weinert edited this page Jul 12, 2018
·
4 revisions
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.
- Create a FluentDOM\Query for the XML
- Register a prefix for the Atom namespace
- Find all title elements and iterate them.
- Output the title element node (will be cast to string)
- 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";
}
Atom-Powered Robots Run Amok
http://example.org/2003/12/13/atom03
- Create a FluentDOM\DOM\Document
- Load the feed
- Register a prefix for the Atom namespace
- Fetch all title elements using FluentDOM\DOM\Document::evaluate() and iterate them.
- Output the title element node (will be cast to string)
- 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";
}
For reference, here is how you could do the same with the PHP DOM classes.
- Create a DOMDocument
- Load the feed
- Create a DOMXpath
- Register a prefix for the Atom namespace
- Fetch all title elements using DOMXpath::evaluate() and iterate them.
- Output the node value of the title element node
- 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";
}
- Home
- Getting Started
- Tasks
- Plugins
- Functions
- Lists
- Creator (5.1)
- CSS Selectors
- Convertors
- Loaders
- Serializers (5.1)
- Transformers (5.1)
- Extended DOM
- XMLReader (6.1)
- XMLWriter (6.1)
- Interfaces