I think using xpath is the best choice here:
<?php $string = <<<XML <?xml version='1.0'?> <document> <title>Forty What?</title> <from>Joe</from> <to>Jane</to> <body> I know that the answer -- but what the question? </body> </document> XML; function get_all_siblings(SimpleXMLElement $node) { return $node->xpath('preceding-sibling::* | following-sibling::*'); } $xml = simplexml_load_string($string); foreach (get_all_siblings($xml->to) as $e) echo $e->getName()."\n"; ?>
Results in:
title from body
source share