I know this is shorter. and cooler...
foreach($s->person as $person) { $firstname_text_value = $person->firstname; }
but this is a whole world clearer...
foreach($s->person->children as $person) { $firstname_text_value = $person->firstname->value; }
Most of the work I do with XML involves object morphing anyway (eg. casting a node into a userspace object)... I do wonder if the enthusasm for the cool things that 'can' be done with the engine are slightly clouding the view of wheter it should be done....
Regards Alan
AMT>> foreach ($dom->getElementsByTagname('person') as $person) {
AMT>> list($firstname) =
$person->getElementsByTagname('firstname');
AMT>> $firstname_text_value = $firstname->firstChild->nodeValue;
AMT>> }
AMT>> AMT>> Compare that to:
AMT>> AMT>> foreach($s->person as $person) {
AMT>> $firstname_text_value = $person->firstname;
AMT>> }
AMT>> AMT>> You tell me which one "nobody can understand."
Both can be understood, but in the first case you know wat is going on (so
you can rely on $s->person being array and you would be able to do:
$elements = $dom->getElementsByTagname('person');
do_something_with($elements[0]);
and in the second case there's so much magic you don't know what the hell is really going on. That's OK when you need to do just that, that stops to
be OK when you want to slightly modify the behaviour and discover the magic is tightly coupled with particular case of use. For example, what exactly is $person->firstname there? Tag, or some attribute, or contents? What is $s->person - is it list of all tags with name 'person' contained in $s or is it element with ID 'person' in $s or maybe it is 'person' attribute of tag $s? How can I see it from that code? I can't.
print($xml->iWant->ToHave->TheFirstName->OfThisPerson());
Yeah that looks pretty stupid...
print($xml->Person->element["Name"]->Attributes["First"]);
or was it
print($xml->Person->element["Name"]->First->__toString);
i don't know!
Thats's the place SimpleXML was designed for. To allow you to write easy code that does the object binding automatically:
print($xml->Person->Name->First);
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php