
La salida XML obtiene la información meteorológica global, regional y nacional para hoy de la página de inicio de uk.weather.com. El resultado se actualiza cada 15 minutos.
Salida:
<?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>18</degrees>
<weather>Sunny</weather>
</city>
<city city="Bristol">
<degrees>18</degrees>
<weather>Fair</weather>
</city>
<city city="Cambridge">
<degrees>14</degrees>
<weather>Cloudy</weather>
</city>
<city city="Cardiff">
<degrees>16</degrees>
<weather>Fair</weather>
</city>
<city city="Coleraine">
<degrees>16</degrees>
<weather>Mostly Cloudy</weather>
</city>
</category>
</data>
</weather>
Código fuente del 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);
}
?>