Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:7443 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 82456 invoked by uid 1010); 1 Feb 2004 00:27:33 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 82432 invoked from network); 1 Feb 2004 00:27:33 -0000 Received: from unknown (HELO miranda.org) (209.58.150.153) by pb1.pair.com with SMTP; 1 Feb 2004 00:27:33 -0000 Received: (qmail 28070 invoked by uid 546); 1 Feb 2004 00:27:32 -0000 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 1 Feb 2004 00:27:32 -0000 Date: Sat, 31 Jan 2004 19:27:32 -0500 (EST) X-X-Sender: adam@miranda.org To: Derick Rethans cc: PHP Developers Mailing List In-Reply-To: Message-ID: References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Subject: Re: [PHP-DEV] SimpleXML->children() and text nodes From: adam@trachtenberg.com (Adam Maccabee Trachtenberg) On Sun, 1 Feb 2004, Derick Rethans wrote: > when I iterate over $s->body->children() I only get the and > the . 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). > > 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 -- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!