SimpleXML: Accessing Unknown Element


PHP SimpleXML Step By Step Tutorial - Part 5: It is easy when we know structure of document. We can navigate to elements just using the element names as properties. But how if we have not known? We can use children() methods that returns an interable SimpleXMLElement object, which allows you to iterate through all the child elements of an element.
<?php
$lib  = simplexml_load_file("test.xml");
$children = $lib->book->children();

print_r($children);
?>

For iteration, we can try like this:

<?php
$lib  = simplexml_load_file("test.xml");
$children = $lib->children();

foreach($children as $node){
  echo $node->title;
  echo "<br />";
}

?>


Series this article:
SimpleXML: Creating a SimpleXMLElement
SimpleXML: Creating XML Document From Database
SimpleXML: Accessing Element
SimpleXML: Using Iterable Object
SimpleXML: Accessing Unknown Element
SimpleXML: Creating File Contain Part of Another XML Document
SimpleXML: Mixing With DOM Interoperability

Bookmark and Share Tag: SimpleXML, SimpleXMLElement, XML, element Category: PHP Basic Post : April 17th 2008 Read: 30,839

advertisements


blog comments powered by Disqus