Hi everyone! This has been causing me massive stress over the past 48 hours, I simply don't know enough about PHP to resolve this problem (which is probably quite simple!), so I thought I'd turn to the experts I have an XML string that I have successfully managed to import and I want to extract one specific element/node from this string, which happens to be a URL. This is the XML string (I removed some of the unrelated elements to make it easier to read): <?xml version="1.0" encoding="utf-8"?> <elements> <facebookilike identifier="78a0daa1-8b7e-4793-9f19-65991d485a3c"> <value><![CDATA[1]]></value> </facebookilike> <text identifier="c5bba97d-1158-4359-9089-9f35b243a60f"> <value/> </text> <text identifier="70e50e8d-2a2d-4ca4-9f4e-0f691aaf7a96"> <value><![CDATA[Olivia Palermo]]></value> </text> <text identifier="b4ba2eaf-ae3c-4e31-81fa-979cfd9fe811"> <value><![CDATA[£590]]></value> </text> <link identifier="d6539bee-d12b-4a66-b8a1-fd0a7c42cda7"> <value><![CDATA[http://www.theitguide.com]]></value> <text/> <target/> <custom_title/> <rel/> </link> <textarea identifier="beab45eb-b32a-4766-822d-b3fb72b5b1d4"> <value><![CDATA[<p>Red satin pumps with ruffle fan back and a heel that measures approximately 150mm/ 6 inches with a 40mm/ 1.5 inch island platform. Charlotte Olympia pumps have an almond toe, satin covered platform and heel, gold metal designer logo at sole, leather inner sole and comes with matching colored stockings with spider web insignia at calf.</p>]]></value> </textarea> <socialbookmarks identifier="00e1df62-828d-42f5-a1a6-a89d553d4934"> <value><![CDATA[1]]></value> </socialbookmarks> <relateditems identifier="64c188a6-cf2f-4895-8879-8b67a58b6780"/> </elements> Code (markup): I want to extract the link 'http://www.theitguide.com' - and only this. I've built a redirection page to get this XML from my database, which looks like this: <?php $item_id = $_GET['pid']; if (is_numeric($item_id)) { $fullURL; $con = mysql_connect(xxxxxx, xxxxxx, xxxxxx); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("xxxxxx", $con); $result = mysql_query("SELECT * FROM jos_zoo_item WHERE id='".$item_id."'"); while($row = mysql_fetch_array($result)) { $URL = $row["elements"]; } --MISSING CODE-- if (strlen($URL) > 0) { $fullURL = $URL; } else { $fullURL = $mosConfig_live_site; } } else { $fullURL = $mosConfig_live_site; } ?> <html> <head> <script type="text/javascript"> <!-- window.location = "<?php echo $fullURL; ?>" //--> </script> PHP: The code I believe I'm missing is to take $xml, extract my URL from it and then create $URL with that URL as the content. Could someone help me out and put me out of my misery Thank you!!! Helen
You can use SimpleXML to extract the url. $xml_doc = simplexml_load_string($xml); $url = $xml_doc->link->value; PHP: But there is a problem with your xml file, when i used 1:1 copy i got warnings at this line <value><![CDATA[£590]]></value> Code (markup): , it's because of pound character, it's not properly encoded. Don't know how much influence you have over xml content, but something would need to be done there. Here is fully working sample: $xml = '<?xml version="1.0" encoding="utf-8"?> <elements> <facebookilike identifier="78a0daa1-8b7e-4793-9f19-65991d485a3c"> <value><![CDATA[1]]></value> </facebookilike> <text identifier="c5bba97d-1158-4359-9089-9f35b243a60f"> <value/> </text> <text identifier="70e50e8d-2a2d-4ca4-9f4e-0f691aaf7a96"> <value><![CDATA[Olivia Palermo]]></value> </text> <text identifier="b4ba2eaf-ae3c-4e31-81fa-979cfd9fe811"> <value><![CDATA[GBP590]]></value> </text> <link identifier="d6539bee-d12b-4a66-b8a1-fd0a7c42cda7"> <value><![CDATA[http://www.theitguide.com]]></value> <text/> <target/> <custom_title/> <rel/> </link> <textarea identifier="beab45eb-b32a-4766-822d-b3fb72b5b1d4"> <value><![CDATA[<p>Red satin pumps with ruffle fan back and a heel that measures approximately 150mm/ 6 inches with a 40mm/ 1.5 inch island platform. Charlotte Olympia pumps have an almond toe, satin covered platform and heel, gold metal designer logo at sole, leather inner sole and comes with matching colored stockings with spider web insignia at calf.</p>]]></value> </textarea> <socialbookmarks identifier="00e1df62-828d-42f5-a1a6-a89d553d4934"> <value><![CDATA[1]]></value> </socialbookmarks> <relateditems identifier="64c188a6-cf2f-4895-8879-8b67a58b6780"/> </elements>'; $xml_doc = simplexml_load_string($xml); $url = $xml_doc->link->value; echo $url; PHP:
Thank you so very much!!!!!!!!!!!!! The '£' symbol was clearly what was causing me a problem - but I didn't know that. And now I've removed it everything works. Thank you, thank you, thank you
It depends where are you getting the xml from, if it's from external source, then they should fix it, because the content (in this case '£') is not utf8 encoded, like it says in xml header. Otherwise if you don't have any other choice, you could run the xml through utf8 function by yourself, but that's really, really BAD solution, because you get mixed content and unreadable characters. You can see proper answer here http://stackoverflow.com/questions/...-8-indicate-encoding-using-phps-simplexml-loa Basically $xml = utf8_encode($xml); but, like already said, it's bad solution, the xml provider should deliver properly encoded content.