bijukon Posted January 17, 2010 Share Posted January 17, 2010 Hi all I'm using simplexml to read xml files in to my database, normally getting xml data is pretty straightforward. I'm having trouble however trying to get data for when it is formatted in the following way: <comments> <comment lang="fr_FR"><![CDATA[lalalalalalalalalalallala]]></comment> <comment lang="en_GB"><![CDATA[hello english descriptions]]></comment> <comment lang="de_DE"><![CDATA[yayayayayayay]]></comment> <comment lang="it_iT"><![CDATA[]]></comment> <comment lang="es_ES"><![CDATA[]]></comment> <comment lang="ru_RU"><![CDATA[]]></comment> <comment lang="no_NO"><![CDATA[]]></comment> </comments> I normally extract data $variable = $advert->reference. I'm having trouble extracting en_gb data. Any guidance would be appreciated. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/188818-reading-xml-with-elements-and-attributes/ Share on other sites More sharing options...
Maq Posted January 17, 2010 Share Posted January 17, 2010 You have to use a combination of the DOM element and XPath. Here is an example using your test data to extract all of the comment values that have a lang attribute value of en_GB. $dom = new DOMDocument; $dom->load('comment.xml'); $xpath = new DOMXPath($dom); $xpath->registerNamespace("php", "http://php.net/xpath"); $nodes = $xpath->query('/comments/comment[@lang = "en_GB"]'); foreach ($nodes as $node) { $value = $node->nodeValue; echo $value; } ?> This assumes the XML file is in the same directory as the PHP script. Let me know if you have any questions. Quote Link to comment https://forums.phpfreaks.com/topic/188818-reading-xml-with-elements-and-attributes/#findComment-996838 Share on other sites More sharing options...
MadTechie Posted January 17, 2010 Share Posted January 17, 2010 Here is a simplexml example to extract all, of course you could add a if statement to find the "en_GB".. $XMLData = <<<XML <comments> <comment lang="fr_FR"><![CDATA[lalalalalalalalalalallala]]></comment> <comment lang="en_GB"><![CDATA[hello english descriptions]]></comment> <comment lang="de_DE"><![CDATA[yayayayayayay]]></comment> <comment lang="it_iT"><![CDATA[]]></comment> <comment lang="es_ES"><![CDATA[]]></comment> <comment lang="ru_RU"><![CDATA[]]></comment> <comment lang="no_NO"><![CDATA[]]></comment> </comments> XML; $XML = simplexml_load_string($XMLData); foreach($XML->comment as $element) { $attr = $element->attributes(); echo "ATTR:".$attr['lang']."<BR />\n"; echo "VALUE:$element<BR />\n"; echo "<hr>"; } Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/188818-reading-xml-with-elements-and-attributes/#findComment-996841 Share on other sites More sharing options...
bijukon Posted January 17, 2010 Author Share Posted January 17, 2010 Thank you very much Maq & MadTechie I've now managed to extract the data as mentioned, I'm using the Medtechie's suggestions as that works with my script without major rewrite, but thanks for the heads up on the Dom Xpath method, I'll certainly remember to consider using it. Thanks again folks. Quote Link to comment https://forums.phpfreaks.com/topic/188818-reading-xml-with-elements-and-attributes/#findComment-996842 Share on other sites More sharing options...
Maq Posted January 17, 2010 Share Posted January 17, 2010 Thank you very much Maq & MadTechie I've now managed to extract the data as mentioned, I'm using the Medtechie's suggestions as that works with my script without major rewrite, but thanks for the heads up on the Dom Xpath method, I'll certainly remember to consider using it. Thanks again folks. Good to hear you you got it working. You can mark the topic as solved by clicking the tab on the bottom left. Quote Link to comment https://forums.phpfreaks.com/topic/188818-reading-xml-with-elements-and-attributes/#findComment-996846 Share on other sites More sharing options...
salathe Posted January 17, 2010 Share Posted January 17, 2010 It's also perfectly possible to take the XPath idea and use it with SimpleXML like: $xml = <<<XML <comments> <comment lang="fr_FR"><![CDATA[lalalalalalalalalalallala]]></comment> <comment lang="en_GB"><![CDATA[hello english descriptions]]></comment> <comment lang="de_DE"><![CDATA[yayayayayayay]]></comment> <comment lang="it_iT"><![CDATA[]]></comment> <comment lang="es_ES"><![CDATA[]]></comment> <comment lang="ru_RU"><![CDATA[]]></comment> <comment lang="no_NO"><![CDATA[]]></comment> </comments> XML; $sxml = simplexml_load_string($xml); $lang = 'en_GB'; $nodes = $sxml->xpath("/comments/comment[@lang='$lang']"); echo "Comment for lang '$lang' is: $nodes[0]"; Quote Link to comment https://forums.phpfreaks.com/topic/188818-reading-xml-with-elements-and-attributes/#findComment-996871 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.