On Sun, 1 Feb 2004, Derick Rethans wrote:

> when I iterate over $s->body->children() I only get the <element/> and
> the <baz/>. Shouldn't it also show the text nodes?

I hestitate to get back into this discussion, but I will give you the
answers as best I can. :)

The SimpleXML model prefers you to not mix element and text nodes
side-by-side. You can, and it won't complain, but (IMHO) the theory is
that an element should either contain other elements or a single text
(or CDATA) node. This is a more "XML-centric" viewpoint towards data
than an "HTML-centric" perspective. (Realize these are generalizations
here about the nature of XML and HTML.)

If you're interested in accessing the XML inside $sx->body, you can
either call $sx->body->asXML(), which will give it to you as a string,
or use DOM. :)

> Another thing that would be useful to have is a tag() method, so that
> this would work too: (ie, I can check what tag I got during iteration).
>
> <?php
>   foreach ($sx->body->children() as $node) {
>     if ($node->tag() == 'element') {
>       /* do this */
>     }
>   }
> ?>
>
> or can I do that in a different way now?

AFAIK, you're generally expected to have an idea of your document's
schema when using SimpleXML. (Alternatively, it's easier to use
SimpleXML when you know the document's schema.)

There's no tag() method, but you can always do:

// My personal favorite
if (isset($node->element)) {
  /* do this */
}

Or:

if ($node->xpath('/element')) {
  /* do this */
}

Or:

$dom = new domDocument;
$dom->import_simplexml($sx);
/* DOM code here */

SimpleXML doesn't really have a good set of intraspection methods
because it's a slippery slope and that ugliness is better left to the
DOM extension. That's the party line, AFAIK.

BTW, I promise in advance to completely retract any or all of my
comments if Sterling, Marcus, or Rob contradict me. :)

-adam

-- 
[EMAIL PROTECTED]
author of o'reilly's php cookbook
avoid the holiday rush, buy your copy today!

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to