nikohak Posted December 17, 2009 Share Posted December 17, 2009 Hi, What do I need to write to get the content of xml page-tag that has id "story2" to appear on the screen, in other words: "head2"? I have the xml already loaded with simplexml into $myXml variable and it works just fine, but this particular thing is giving me headache. So it's something like: echo $myXml->page... Here is the XML: <?xml version="1.0" encoding="utf-8"?> <content> <page id="story1" langurl="1">head1</page> <page id="story2" langurl="2">head2</page> <page id="story3" langurl="3">head3</page> <page id="story4" langurl="4">head4</page> <page id="story5" langurl="5">head5</page> <page id="story6" langurl="6">head6</page> </content> Link to comment https://forums.phpfreaks.com/topic/185474-simplexml-accessign-data/ Share on other sites More sharing options...
rajivgonsalves Posted December 17, 2009 Share Posted December 17, 2009 this should help, not too sure if this is the most optimal way to do it though <?php $str = '<?xml version="1.0" encoding="utf-8"?> <content> <page id="story1" langurl="1">head1</page> <page id="story2" langurl="2">head2</page> <page id="story3" langurl="3">head3</page> <page id="story4" langurl="4">head4</page> <page id="story5" langurl="5">head5</page> <page id="story6" langurl="6">head6</page> </content> '; $xml = simplexml_load_string($str); for ($i=0;$i<count($xml->page);$i++) { $attribs = getAttributes($xml->page[$i]); if ($attribs['id'] == 'story2') { echo $xml->page[$i]; } } function getAttributes(&$node) { $attribs = array(); foreach ($node->attributes() as $name => $attr) { $attribs[$name] = (string) $attr[0]; } return $attribs; } ?> Link to comment https://forums.phpfreaks.com/topic/185474-simplexml-accessign-data/#findComment-979215 Share on other sites More sharing options...
nikohak Posted December 17, 2009 Author Share Posted December 17, 2009 ok, I will take a look at that. Somehow I would have thought that this is so common that there would be a ready syntax for this. Something like: $myXml->page[id=story1] ? Link to comment https://forums.phpfreaks.com/topic/185474-simplexml-accessign-data/#findComment-979218 Share on other sites More sharing options...
rajivgonsalves Posted December 17, 2009 Share Posted December 17, 2009 actually there is salathe had pointed it out earlier in one of my other post print_r($xml->xpath('//content/page[@id="story2"]')) Link to comment https://forums.phpfreaks.com/topic/185474-simplexml-accessign-data/#findComment-979224 Share on other sites More sharing options...
nikohak Posted December 17, 2009 Author Share Posted December 17, 2009 Ok that seems to work. Can I squeeze the last detail from you? That code gives an array, so if I read it into a variable and then do $var[0] it'll give me what I need. But can I somehow get it directly. Link to comment https://forums.phpfreaks.com/topic/185474-simplexml-accessign-data/#findComment-979247 Share on other sites More sharing options...
rajivgonsalves Posted December 17, 2009 Share Posted December 17, 2009 Yeah you could try this echo array_pop($xml->xpath('//content/page[@id="story2"]')) Link to comment https://forums.phpfreaks.com/topic/185474-simplexml-accessign-data/#findComment-979250 Share on other sites More sharing options...
nikohak Posted December 17, 2009 Author Share Posted December 17, 2009 Thanks for this. That works just perfectly! Link to comment https://forums.phpfreaks.com/topic/185474-simplexml-accessign-data/#findComment-979352 Share on other sites More sharing options...
salathe Posted December 17, 2009 Share Posted December 17, 2009 [ot]Or current, that's my fav. for grabbing the XPath match. P.S. Lots of (Simple)XML questions lately. [/ot] Link to comment https://forums.phpfreaks.com/topic/185474-simplexml-accessign-data/#findComment-979356 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.