simpleXML expressions
| From: | Jason Barnett | Date: | Sat, 03 Jul 2004 08:02:35 +0000 |
| Subject: | simpleXML expressions | ||
| Groups: | php.xml.dev | ||
| Request: | Send a blank email to php-xml-dev+get-405@lists.php.net to get a copy of this message | ||
Hi all... I'm trying to understand the new simpleXML model. How do the iterator methods used with simpleXML work? My guess is something like the following, but I'd appreciate it if someone could verify if my understanding of XPath is correct (or if this is done in some other way!?)
Jason
<?php
$xml = '<root><foo><bar/><baz/></foo></root>';
$sxe = simpleXML_load_string($xml);
// The normal way of accessing simpleXMLElement
foreach ($sxe->foo as $node)
foreach ($node as $node2)
$list1[] = $node2;// My guess as to what kind of XPath matches this // But shouldn't the xpath be /foo/* (root->foo->any) // When I tried /foo/*, I got an error foreach ($sxe->xpath('foo/*') as $node) $list2[] = $node; // And now we test if the two lists are the same echo $list1 == $list2 ? "They are equal\n" : "They are not equal\n"; echo '<pre>'; print_r($list1); print_r($list2); echo '</pre>'; // Finally, we test that the first element should be: echo $list1[0] == $sxe->xpath('/foo/*') ? "They are equal\n" : "They are not equal\n"; ?>