Heres how to do it - [Caution - this code is unchecked]
//assume places.xml contains multiple places, each with a name and description
$placesxml = simplexml_load_file("places.xml");
$places = $placesxml->xpath("//Place");
// $places is now an array of XML elements, each a Place element
foreach ($places as $place) {
//$place now points to each Place in the XML document in turn
// so we can use object references to access the elements within a Place
print $place->name, $place->description;
// or even XPath expression as well
// and of course, if the are repeated elements in Place, such Link, with elements
//url and text, we can use a inner loop to work with these
foreach( $place->Link as $link) {
print "<a href='$link->url'/>$link->text</a>"
}
}
1 comment:
God artikel.
Sad og legede lidt med Node-lister og Binære-datastruktur i PHP. Og så manglede jeg nogle at medlem lagre node strukturen på.. og selvfølgelig: XML, OOPHP..
Hilsen Johs.
Post a Comment