phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


SimpleXML: Accessing Unknown Element

Tag: SimpleXML, SimpleXMLElement, XML, element   Category: PHP Basic
post: 17 Apr 2008 read: 667


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

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Joomla: Fast Road to Understand Component Programming
Chart: How to Build Cool Animation Real Time Chart
Email: Send Attachement Mail
SMS : Sending SMS with PHP and ActiveXperts (Part 1)

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


624
posting