Example - xml creator

XML output retrieves today's national, regional and global weather from homepage of uk.weather.com. Output is refreshing each 15 minutes from cron.

 

 

Output:

<?xml version="1.0" encoding="UTF-8"?>
<weather>
  <data title="UK and World Weather Forecast: radar, severe warnings, and more - weather.co.uk">
    <category category_name="National Weather">
      <city city="Brighton">
        <degrees>-4</degrees>
        <weather>Clear</weather>
      </city>
      <city city="Bristol">
        <degrees>-4</degrees>
        <weather>Partly Cloudy</weather>
      </city>
      <city city="Cambridge">
        <degrees>-10</degrees>
        <weather>Clear</weather>
      </city>
      <city city="Cardiff">
        <degrees>-2</degrees>
        <weather>Cloudy</weather>
      </city>
      <city city="Coleraine">
        <degrees>8</degrees>
        <weather>Cloudy</weather>
      </city>
    </category>
  </data>
</weather>

Source code of script:

# File: xmlcreator_main.w
# Name: XML Creator
# Description: Script opens defined URL and saves several fields into XML file.
# Input: URL
# Output format: XML
# Output fields: Title, Category, City, Degrees, Weather

#<Logger File>
#	Global
#	FileName xmlcreator_log.log
#	Level debug
#</Logger>

<Section>
	Name xmlcreator_main
			
	Define $output_file xmlcreator_output.xml

	# clean output file
    <Action Print>
        FileName {$output_file}
        FileMode Write  
    </Action>
	
    # load content    
    <Action ContentURL>
        URL http://uk.weather.com/
        RemoveNewLine
    </Action>
	
	# inicializing of XML structure
	<Action Php>
		Code $context->setVariable('$dom', createXML());
	</Action>

	<Pattern>
		RegExp <TITLE>{$title_tag}</TITLE>
		Compact
		Trim
	</Pattern>

	# add title to structure
	<Action Php>
		Code $context->setVariable('$dom', addTitleToXML($context->getVariable('$dom'),$context));
	</Action>

	<Section While>
		Optional
		NoContext
		
		<Pattern>
			RegExp <h2>{$h2_tag}</h2>*<div class="cities_table">
			Compact
			Trim
		</Pattern>
				
		<Section While>
			Optional
			NoContext
			EndAt <div class="cities_footer">
			
			<Pattern>
				RegExp <td{:re([^>]*)}><a href="{:re([^"]*)}">{@city}</a></td>*</tr>*<tr>*<td{:re([^>]*)}>{@weather}</td>*<td{:re([^>]*)}>{@degrees}</td>
				Compact
				Trim
			</Pattern>
		</Section>
	
		# add data from this section iteration to structure
		<Action Php>
			Code $context->setVariable('$dom', addSectionToXML($context->getVariable('$dom'),$context));
		</Action>

	</Section>

	# save whole XML structure to output file
	<Action Php>
		Code saveXML($context->getVariable('$dom'),$context->getVariable('$output_file'));
	</Action>
</Section>	

Main xmlcreator_main
<?php

function createXML() {
	$doc = new DOMDocument('1.0','UTF-8');
	$doc->formatOutput = true;
	$root = $doc->createElement('weather');
	$root = $doc->appendChild($root);
	return $doc;
}

function addTitleToXML($doc, $context) {
	$r = $doc->getElementsByTagName('weather');
	$main = $r->item(0);
	$root = $doc->createElement('data');
	$root = $main->appendChild($root);
	$root->setAttribute("title", $context->getVariable('$h1_tag'));

	return $doc;
}

function addSectionToXML($doc, $context) {
	$r = $doc->getElementsByTagName('data');
	$main = $r->item(0);

	$node = $doc->createElement('category');
	$node = $main->appendChild($node);
	$node->setAttribute("category_name", $context->getVariable('$h2_tag'));

	if ($context->getVariable('@city')!="") {
		$city = $context->getVariable('@city');
		$degrees = $context->getVariable('@degrees');
		$weather = $context->getVariable('@weather');
		for($i=0;$i<=count($city)-1;$i++) {
			$subnode = $doc->createElement('city');
			$subnode = $node->appendChild($subnode);
			$subnode->setAttribute("city", $city[$i]);

			$sub2node = $doc->createElement('degrees');
			$sub2node = $subnode->appendChild($sub2node);
			if ($degrees[$i]!="N/A") $degrees[$i] = (int)$degrees[$i];
			$data = $doc->createTextNode($degrees[$i]);
			$data = $sub2node->appendChild($data);

			$sub2node = $doc->createElement('weather');
			$sub2node = $subnode->appendChild($sub2node);
			$data = $doc->createTextNode($weather[$i]);
			$data = $sub2node->appendChild($data);
		}
	}

	$context->clearVariable('@city');
	$context->clearVariable('@degrees');
	$context->clearVariable('@weather');
	return $doc;
}

function saveXML($doc, $output) {
	$doc->save($output);
}

?>
Stay in touch with UnitMiner
© 2004-2012 QualityUnit.com, All rights reserved