-
Notifications
You must be signed in to change notification settings - Fork 20
Element Traversable
Thomas Weinert edited this page Jul 12, 2018
·
5 revisions
FluentDOM\Element
implements the Traversable interface. More specific it implements IteratorAggregate. The returned
Iterator is a RecursiveIterator. So it can be used it iterate the child nodes or all descendants.
$xml = <<<'XML'
<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>
XML;
$document = new FluentDOM\DOM\Document();
$document->preserveWhiteSpace = FALSE;
$document->loadXML($xml);
foreach ($document->documentElement as $node) {
echo get_class($node), " ";
if ($node instanceof DOMElement) {
echo ' - ', $node->nodeName;
} elseif ($node instanceof DOMText) {
echo ': ', $node->nodeValue;
}
echo "\n";
}
FluentDOM\Element - title
FluentDOM\Element - link
FluentDOM\Element - updated
FluentDOM\Element - author
FluentDOM\Element - id
FluentDOM\Element - entry
$xml = <<<'XML'
<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>
XML;
$document = new FluentDOM\DOM\Document();
$document->preserveWhiteSpace = FALSE;
$document->loadXML($xml);
$iterator = new RecursiveIteratorIterator(
$document->documentElement, RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $node) {
echo get_class($node), " ";
if ($node instanceof DOMElement) {
echo ' - ', $node->nodeName;
} elseif ($node instanceof DOMText) {
echo ': ', $node->nodeValue;
}
echo "\n";
}
FluentDOM\Element - title
FluentDOM\Text : Example Feed
FluentDOM\Element - link
FluentDOM\Element - updated
FluentDOM\Text : 2003-12-13T18:30:02Z
FluentDOM\Element - author
FluentDOM\Element - name
FluentDOM\Text : John Doe
FluentDOM\Element - id
FluentDOM\Text : urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6
FluentDOM\Element - entry
FluentDOM\Element - title
FluentDOM\Text : Atom-Powered Robots Run Amok
FluentDOM\Element - link
FluentDOM\Element - id
FluentDOM\Text : urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a
FluentDOM\Element - updated
FluentDOM\Text : 2003-12-13T18:30:02Z
FluentDOM\Element - summary
FluentDOM\Text : Some text.
- 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