...

View Full Version : Creating Link from XML document



alexc_6311
11-13-2012, 01:02 PM
Im trying to learn to build a new part of my website retriving information from an external xml document.

I can do all the listing bits and name codes etc etc but Im wondering how I create the retrived slug as a link. Eg. the xml document has

<slug>tm_44_compliance</slug>

My php code looks like this:

<?php
//create new document object
$dom_object = new DOMDocument();
//load xml file
$dom_object->load("http://quidos.eventhq.co.uk/.xml");

$event = $dom_object->getElementsByTagName("event");

foreach( $event as $activity )
{
$names = $activity->getElementsByTagName("name");
$name = $names->item(0)->nodeValue;


$summarys = $activity->getElementsByTagName("summary");
$summary = $summarys->item(0)->nodeValue;

$slugs = $activity->getElementsByTagName("slug");
$slug = $slugs->item(0)->nodeValue;

echo "$name - $summary - $slug<br>";
}
?>

How do I enter in that I want the slug to appear as a link eg.

http://quidos.eventhq.co.uk/{slug}

Thanks

Fumigator
11-13-2012, 05:49 PM
The trick is in the echo, which you may recall is dumping output to your browser, which can read HTML. So instead of



echo "$name - $summary - $slug<br>";


You want to HTML-ize that up like so:



echo "<p>$name - $summary - <a href='http://quidos.eventhq.co.uk/$slug'>$slug</a></p>";


As a side note, it's interesting you're using the DOMDocument Object to parse XML, when there are other tools in PHP better suited for parsing XML, such as the SimpleXML object (http://us2.php.net/manual/en/simplexml.examples-basic.php).

alexc_6311
11-14-2012, 03:46 PM
Thanks I've not got my links working!

I have also transfered away from DOMDoc to SimpleXML

DOM was first method I came across via google.

Thanks

alexc_6311
11-14-2012, 04:45 PM
Still appears to not be working using SimpleXML

Can someone spot the mistake?



<?php
// load SimpleXML
$events = new SimpleXMLElement('http://quidos.eventhq.co.uk/.xml', null, true);

echo <<<EOF
<table border="1">
<tr>
<th>Name</th>
<th>Summary</th>
<th>Location</th>
<th>City</th>
<th>Start Date</th>
<th>Finish Date</th>
<th>Book</th>
</tr>

EOF;
foreach($events as $event) // loop through our events
{
echo <<<EOF
<tr>
<td>{$event->name}</td>
<td width="250">{$event->summary}</td>
<td>{$event->location}</td>
<td>{$event->city}</td>
<td>{$event->event_start_date}</td>
<td>{$event->event_end_date}</td>
<td>{$event->quidos.eventhq.co.uk/slug}</td>
</tr>

EOF;
}
echo '</table>';
?>

Fumigator
11-15-2012, 12:04 AM
How is it not working? (Your anchor tags didn't make the last code snippet you posted, btw.)

minder
11-15-2012, 01:06 AM
I copied your xml file and the below writes all the values in the <slug> elements out as links


<?php
$xmlData = simplexml_load_file('XML_data.xml');
if(!$xmlData){
die('xml load failed');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
</head>
<body>
<table>
<?php
foreach($xmlData->events->event as $event){
echo '<tr>';
echo '<td><a href="#">'.$event->slug.'</a></td>';
echo '</tr>';
}
?>
</table>
</body>
</html>


<?xml version="1.0" encoding="UTF-8"?>
<root>
<events>
<event>
<name>TM44 Compliance Training Course</name>
<slug>tm44_compliance_training_course_birmingham_nov</slug>
<summary>Training day detailing how to complete Air-Conditioning Inspections in line with new TM44 requirements.</summary>
<location>Villa Park</location>
<city>Birmingham</city>
<country>GB</country>
<open_registration_date>2012-10-09 12:12:00</open_registration_date>
<close_registration_date>2012-11-15 12:12:00</close_registration_date>
<event_start_date>2012-11-15 10:00:00</event_start_date>
<event_end_date>2012-11-15 16:30:00</event_end_date>
</event>
<event>
<name>Quidos Domestic Green Deal Advisor Training</name>
<slug>quidos_domestic_green_deal_advisor_training_bath_november</slug>
<summary>Training to become a domestic Green Deal Advisor. </summary>
<location>Quidos Bath</location>
<city>Bath</city>
<country>GB</country>
<open_registration_date>2012-09-06 12:53:00</open_registration_date>
<close_registration_date>2012-11-22 12:53:00</close_registration_date>
<event_start_date>2012-11-20 10:00:00</event_start_date>
<event_end_date>2012-11-22 16:30:00</event_end_date>
</event>
<event>
<name>Quidos Floor Plan Training</name>
<slug>quidos_floor_plan_training_november_2012_bath</slug>
<summary>A thorough introduction into sketching floor plans. Valid for 5 hours worth of CPD. </summary>
<location>Quidos Bath</location>
<city>Bath</city>
<country>GB</country>
<open_registration_date>2012-04-21 00:00:00</open_registration_date>
<close_registration_date>2012-11-20 17:00:00</close_registration_date>
<event_start_date>2012-11-20 10:00:00</event_start_date>
<event_end_date>2012-11-20 17:00:00</event_end_date>
</event>
<event>
<name>DEA Refresher day</name>
<slug>dea_refresher_day_october_2012_bath</slug>
<summary>Aimed specifically at DEAs, offering expert insight into the industry. Valid for 5 hours of CPD. </summary>
<location>Quidos Offices</location>
<city>Bath</city>
<country>GB</country>
<open_registration_date>2012-04-21 00:00:00</open_registration_date>
<close_registration_date>2012-11-21 17:00:00</close_registration_date>
<event_start_date>2012-11-21 10:00:00</event_start_date>
<event_end_date>2012-11-21 17:00:00</event_end_date>
</event>
<event>
<name>NDEA Refresher day</name>
<slug>ndea_refresher_day_october_2012_bath</slug>
<summary>Aimed specifically at NDEAs, offering expert insight into the industry. Valid for 5 hours of CPD. </summary>
<location>Quidos Offices</location>
<city>Bath</city>
<country>GB</country>
<open_registration_date>2012-04-21 00:00:00</open_registration_date>
<close_registration_date>2012-11-22 17:00:00</close_registration_date>
<event_start_date>2012-11-22 10:00:00</event_start_date>
<event_end_date>2012-11-22 17:00:00</event_end_date>
</event>
<event>
<name>L3 ABBE Certificate in Domestic Energy Assessment</name>
<slug>l3_abbe_certificate_in_domestic_energy_assessment_mdecember_2012</slug>
<summary>Registration for qualification in L3 Domestic Energy Assessment.</summary>
<location>Quidos Bath</location>
<city>Bath</city>
<country>GB</country>
<open_registration_date>2012-04-21 00:00:00</open_registration_date>
<close_registration_date>2012-12-04 17:00:00</close_registration_date>
<event_start_date>2012-12-04 10:00:00</event_start_date>
<event_end_date>2012-12-06 17:00:00</event_end_date>
</event>
<event>
<name>Green Deal Advisor Assessment Day</name>
<slug>green_deal_advisor_assessment_day</slug>
<summary>Green Deal Advisor Assessment Day.</summary>
<location>East of England</location>
<city>TBA</city>
<country>GB</country>
<open_registration_date>2012-11-08 11:26:00</open_registration_date>
<close_registration_date>2012-12-14 11:26:00</close_registration_date>
<event_start_date>2012-12-10 10:00:00</event_start_date>
<event_end_date>2012-12-14 17:00:00</event_end_date>
</event>
<event>
<name>Quidos Domestic Green Deal Advisor Training</name>
<slug>quidos_domestic_green_deal_advisor_training_bath_december</slug>
<summary>Training to become a Domestic Green Deal Advisor.</summary>
<location>Quidos Bath</location>
<city>Bath</city>
<country>GB</country>
<open_registration_date>2012-09-06 12:30:00</open_registration_date>
<close_registration_date>2012-12-18 12:30:00</close_registration_date>
<event_start_date>2012-12-18 10:00:00</event_start_date>
<event_end_date>2012-12-20 16:30:00</event_end_date>
</event>
<event>
<name>Quidos Floor Plan Training</name>
<slug>quidos_floor_plan_training_december_2012_birmingham</slug>
<summary>A thorough introduction into sketching floor plans. Valid for 5 hours worth of CPD. </summary>
<location>Quidos Birmingham</location>
<city>Birmingham</city>
<country>GB</country>
<open_registration_date>2012-04-21 00:00:01</open_registration_date>
<close_registration_date>2012-12-18 17:00:00</close_registration_date>
<event_start_date>2012-12-18 10:00:00</event_start_date>
<event_end_date>2012-12-18 17:00:00</event_end_date>
</event>
<event>
<name>DEA Refresher day</name>
<slug>dea_refresher_day_december_2012_birmingham</slug>
<summary>Aimed specifically at DEAs, offering expert insight into the industry. Valid for 5 hours of CPD. </summary>
<location>Quidos Birmingham</location>
<city>Birmingham</city>
<country>GB</country>
<open_registration_date>2012-04-21 00:00:01</open_registration_date>
<close_registration_date>2012-12-19 17:00:00</close_registration_date>
<event_start_date>2012-12-19 10:00:00</event_start_date>
<event_end_date>2012-12-19 17:00:00</event_end_date>
</event>
<event>
<name>NDEA Refresher day</name>
<slug>ndea_refresher_day_december_2012_birmingham</slug>
<summary>Aimed specifically at NDEAs, offering expert insight into the industry. Valid for 5 hours of CPD. </summary>
<location>Quidos Birmingham</location>
<city>Birmingham</city>
<country>GB</country>
<open_registration_date>2012-04-21 00:00:01</open_registration_date>
<close_registration_date>2012-12-20 17:00:00</close_registration_date>
<event_start_date>2012-12-20 10:00:00</event_start_date>
<event_end_date>2012-12-20 17:00:00</event_end_date>
</event>
</events>
</root>

alexc_6311
11-15-2012, 10:05 AM
Sorted now!! Thanks all



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum