Using PHP to Extract an Element from an XML String

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hhackwell
Forum Newbie
Posts: 1
Joined: Mon Jan 31, 2011 10:21 am

Using PHP to Extract an Element from an XML String

Post by hhackwell »

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

Code: Select all

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

Code: Select all

<?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>
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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Using PHP to Extract an Element from an XML String

Post by Jonah Bron »

The contents of that node can be accessed with $element->nodeValue. So, you'll need to get the root element with $document->documentElement, look for the first <link> tag with getElementsByTagName('link')->item(0), grab the <value> tag inside of it with getElementsByTagName('value')->item(0), and finally access that node's value with nodeValue.

DOMDocument::documentElement
DOMElement::getElementsByTagName
DOMNodeList::item
DOMNode::nodeValue
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Using PHP to Extract an Element from an XML String

Post by Weirdan »

xpath would be easier though:

Code: Select all

$xml = simplexml_load_string($yourstring);
$nodes = $xml->xpath('//link[1]/value')
$url = (string) $nodes[0];
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Using PHP to Extract an Element from an XML String

Post by Jonah Bron »

:roll:
Post Reply