Jump to content

Extract Element from XML String using PHP


hhackwell

Recommended Posts

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  :D

 

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>

 

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(xxxxx, 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))
{
  	$xml = $row["elements"];
  	}



--MISSING CODE--



echo $URL;

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>

 

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

Link to comment
Share on other sites

Hi Helen, welcome to PHPFreaks. :)

 

Have you tried any of PHP's XML classes/functions yourself?  The task is pretty simple but without knowing what, if anything, you've tried so far it's difficult to know where to pitch an answer.

 

Boil the problem down to something basic that you can figure out, without the distraction of the rest of your script.

 

<?php

$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[£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>';

// You figure out something here, if you can.
// Start at http://php.net/refs.xml and SimpleXML is the easiest option.
// Post back if you're falling flat on your face, or get more stuck!

echo $url;

 

Cheerio for now.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.