<?php
$xml = <<<'XML'
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:g="http://base.google.com/ns/1.0">
<title>The name of your data feed</title>
<link href="http://www.example.com" rel="alternate" type="text/html" />
<updated>2006-06-11T18:30:02Z</updated>
<author>
<name>Google</name>
</author>
<id>tag:example.com,2006-06-11:/support/products</id>
<entry>
<title>Red wool sweater</title>
<id>1a</id>
<link href="http://www.example.com/item1-info-page.html" />
<summary>Comfortable and soft, this sweater will keep you warm on those cold winter nights.</summary>
<updated>2005-10-13T18:30:02Z</updated>
<g:image_link>http://www.google.com/images/google_sm.gif</g:image_link> <g:price>25</g:price> <g:condition>new</g:condition>
</entry>
</feed>
XML;
$file = 'data://text/plain;base64,'.base64encode($xml);
$fileSize = strlen($xml);
$readBytes = 0;
$reader = new XMLReader;
$reader->open($file);
$dom = new DOMDocument();
$xpath = new DOMXpath($dom);
$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
$xpath->registerNamespace('gi', 'http://base.google.com/ns/1.0');
// look for the first entry element
while ($reader->read() && $reader->localName !== 'entry') {
continue;
}
// while you have an entry element
while ($reader->localName === 'entry') {
$node = $dom->importNode($reader->expand(), TRUE);
var_dump(
[
'title' => $xpath->evaluate('string(atom:title)', $entry),
'summary' => $xpath->evaluate('string(atom:summary)', $entry),
'image-link' => $xpath->evaluate('string(gi:image_link)', $entry)
]
);
$readBytes += strlen($reader->readOuterXml());
printf(
'Read %s of %s bytes, %d%%',
$readBytes,
$fileSize,
round($readBytes * 100 / $fileSize)
);
// move to the next entry sibling
$reader->next('entry');
}
preferences:
45.92 ms | 402 KiB | 5 Q