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://[email protected]</website> <email>special [email protected]</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. Link to comment https://forums.phpfreaks.com/topic/136682-getelementsbytagname-xml-php5-et-al/ 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://[email protected]</website> <email>special [email protected]</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; ?> Link to comment https://forums.phpfreaks.com/topic/136682-getelementsbytagname-xml-php5-et-al/#findComment-713719 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. :-\ Link to comment https://forums.phpfreaks.com/topic/136682-getelementsbytagname-xml-php5-et-al/#findComment-714487 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.