Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Sunday, March 14, 2010

php - create xml file


$outDom = new DOMDocument('1.0','UTF-8');
$outDom->formatOutput = true;
$outRoot = $outDom->appendChild($outDom->createElement('blogs'));

$blogsLinks = getBlogLinks();

//insert in db
$result = mysql_query("insert into updates(Date) values('".date("Y-m-d h:i:s")."')");
$updateIDs = mysql_query("select max(UpdateID) from updates");
while ($row = mysql_fetch_array($updateIDs, MYSQL_NUM))
{
$updateID = $row[0];
}


foreach($blogsLinks as $link)
{
$outRoot->appendChild(parseXmlDocument($link, $outDom, $outRoot, $updateID));
}
$outDom->save('result.xml');

Read xml with php

An example to parse xml with php.

We have the following xml:


< catalogues >
< services >
< service code="RCA01" name="Consultatie initiala" servicetype="RECI" tariff="14.42" validfrom="2005-01-01">
< service code="RCA02" name="Consultatie de control" servicetype="RECC" tariff="9.56" validfrom="2005-01-01">
< / services>
< / catalogues>





$document = new DOMDocument();
$document->load( 'Nomenclators.xml' );

$sites = $document->getElementsByTagName( "Service" );
echo "Begin load";
foreach( $sites as $site )
{
$code = $site->getAttribute("code");
$name = $site->getAttribute("name");
$tariff = $site->getAttribute("tariff");
$serviceType = $site->getAttribute("serviceType");
$validFrom = $site->getAttribute("validFrom");
$validTo = $site->getAttribute("validTo");
}
echo "End load";