Accessing Elements

Use of SimpleXML looks a bit like XPath expressions, except that the same effect is accomplished programmatically through the API rather than via formatted expressions stored in strings. Unlike XPath expressions, SimpleXML automatically assumes that element access is relative to the root node.

<?php
$markupString = '

<html>
<body>
<ul id="thelist">
<li>Foo</li>
<li>Bar</li>
</ul>
</body>
</html>

// Outputs "Foo" -- note that the "html" element isn't referenced
$sxe = new SimpleXMLElement($markupString); echo $sxe->body->ul->li[0];

// Also works, assumes the first "li" element if several are present
echo $sxe->body->ul->li;

// Also outputs "Foo" -- note that "html" is referenced this time
$doc = new DOMDocument();
$doc->loadHTML($markupString);
$xpath = new DOMXPath($doc);
echo $xpath->evaluate('/html/body/ul/li[0]');

// Outputs "ul"
echo $sxe->body->ul->getName(), PHP_EOL;

// Outputs "Foo" then "Bar" foreach ($sxe->body->ul->li as $li) {
echo $li, PHP_EOL;
}

// Does the same thing
foreach ($sxe->body->ul->children() as $li) {
echo $li, PHP_EOL;
}
?>

When referencing a child element of the current element (via the body property of $sxe above, for example), note that the child element being accessed is also an instance of SimpleXMLElement. This means it’s not only possible to chain element access, but also to reference the last element in such a chain using a variable. See the first foreach loop shown above for an example of both of these.


© SimpleXML Extension — Web Scraping

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (03.09.2014)
Views: 351 | Rating: 0.0/0
Total comments: 0
avatar