<?php
$result = array();
$result[] = array(
'position' => 'some job',
'postdate' => '01/01/2009',
'jobref' => 'abc12345',
'jobid' => '0987654321',
'description' => 'this is a description'
);
$result[] = array(
'position' => 'some other job',
'postdate' => '01/02/2009',
'jobref' => 'abc1212341234345',
'jobid' => '09876123454321',
'description' => 'this is another description'
);
$result[] = array(
'position' => 'some asdfasdfjob',
'postdate' => '01/01/2037',
'jobref' => 'abc1234sdfgsdf5',
'jobid' => 'sdfg0987654321',
'description' => 'this is the last description'
);
$fullurl = '';
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
//Create the source node
$src = $doc->createElement( 'source' );
$doc->appendChild( $src );
//create the source/publisher node
$pub = $doc->createElement( 'publisher' );
$src->appendChild( $pub );
//create the source/publisherurl node
$pubUrl = $doc->createElement( 'publisherurl' );
$src->appendChild( $pubUrl );
//Loop over sql results and create source/job nodes
foreach( $result as $row )
{
//Create /source/job node
$job = $doc->createElement( 'job' );
//Create source/job[]/title node
$title = $doc->createElement( 'title' );
$title->appendChild( $doc->createTextNode( $row['position'] ) );
$job->appendChild( $title );
//Create source/job[]/date node
$date = $doc->createElement( 'date' );
$date->appendChild( $doc->createTextNode( $row['postdate'] ) );
$job->appendChild( $date );
//Create source/job[]/referencenumber node
$ref = $doc->createElement( 'referencenumber' );
$ref->appendChild( $doc->createTextNode( $row['jobref'] ) );
$job->appendChild( $ref );
//Create source/job[]/url node
$url = $doc->createElement( 'url' );
$url->appendChild( $doc->createTextNode( '<a href="http://www.*****.co.uk' . $fullurl . '/info_jobid_' . $row['jobid'] . '.html" target="job' . $row['jobid'] . '">' . $row['jobref'] . '</a>' ) );
$job->appendChild( $url );
//Create source/job[]/country node
$country = $doc->createElement( 'country' );
$country->appendChild( $doc->createTextNode( 'United Kingdom' ) );
$job->appendChild( $country );
//Create source/job[]/description node
$desc = $doc->createElement( 'description' );
$desc->appendChild( $doc->createTextNode( $row['description'] ) );
$job->appendChild( $desc );
$src->appendChild( $job );
}
echo $doc->saveXML();