GURAQTINVU Posted December 12, 2008 Share Posted December 12, 2008 OK, so I have my xml file: <?xml version="1.0" encoding="utf-8"?> <details> <nameone> <paras>1</paras> <paras><em>2</em></paras> <paras>3</paras> </nameone> <nametwo> <paras>1</paras> <paras>2</paras> <paras><em>3</em></paras> <website>http://www.us@here.co.uk</website> <email>special me@here.co.uk</email> </nametwo>< </details> I can do what I want with: $xml = simplexml_load_file($file); $myVar = ""; foreach ($xml->nameone->para as $para) {$myVar .= "<p>".$para."</p>";} echo $myVar for example, but this doesn't allow for nested 'ad lib' tags (please see my cunning nested <em>). What I need is a way of requesting all nodes and values beneath a node, <nameone> for instance, so that all nodes beneath are just included ver batum. Does anyone know an easy and neat way - I'd be grateful. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 12, 2008 Share Posted December 12, 2008 xpath is amazing <?php $xml = <<<XML <details> <nameone> <paras>1</paras> <paras><em>2</em></paras> <paras>3</paras> </nameone> <nametwo> <paras>1</paras> <paras>2</paras> <paras><em>3</em></paras> <website>http://www.us@here.co.uk</website> <email>special me@here.co.uk</email> </nametwo> </details> XML; $xml = simplexml_load_string($xml); $eles = $xml->xpath('/details/child::*/paras'); $myVar = ''; foreach($eles as $para){ $myVar .= "<p>".$para."</p>"; } echo $myVar; ?> Quote Link to comment Share on other sites More sharing options...
GURAQTINVU Posted December 13, 2008 Author Share Posted December 13, 2008 Thanks, you are right about xpath but I seem to struggle at every turn as I am now left with an array of SimpleXMLElement objects (which exactly defines the xml I want apart from the fact that it returns a array with only <em> having an associative index - em) and cannot find a way to turn that array into a string of tags and text. :-\ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.