Jump to content

on the fly xml generation


graham23s

Recommended Posts

Hi Guys,

 

i'm using fwrite to generate some xml for the image gallery, the structure of the gallery xml is:

 

Gallery XML:

 


<gallery title="Miracle Lift - Before And After Photos" thumbDir="./images/thumbs/" imageDir="./images/" random="true">
<category name="Introducing Seattle">
	<image>
		<date>03.31.06</date>
		<title>View of Seattle</title>
		<desc>View of Seattle</desc>
		<thumb>seattle5.jpg</thumb>
		<img>seattle5.jpg</img>
	</image>
	<image>
		<date>03.31.06</date>
		<title>View of Seattle</title>
		<desc>View of Seattle</desc>
		<thumb>seattle7.jpg</thumb>
		<img>seattle7.jpg</img>
	</image>
	<image>
		<date>03.31.06</date>
		<title>My Town</title>
		<desc>On the way home</desc>
		<thumb>seattle10.jpg</thumb>
		<img>seattle10.jpg</img>
	</image>
</category>
<category name="Wedding">
	<image>
		<date>02.44.04</date>
		<title>La Jolla Wedding</title>
		<desc>La Jolla Wedding</desc>
		<thumb>wedding1.jpg</thumb>
		<img>wedding1.jpg</img>
	</image>
	<image>
		<date>05.13.05</date>
		<title>Amazing Kiss</title>
		<desc>Amazing Kiss</desc>
		<thumb>wedding2.jpg</thumb>
		<img>wedding2.jpg</img>
	</image>
	<image>
		<date>11.21.05</date>
		<title>Wedding Time</title>
		<desc>Bride smiling</desc>
		<thumb>wedding3.jpg</thumb>
		<img>wedding3.jpg</img>
	</image>
	<image>
		<date>02.44.04</date>
		<title>10th anniversary</title>
		<desc>Picture taken on our 10th anniversary.</desc>
		<thumb>wedding4.jpg</thumb>
		<img>wedding4.jpg</img>
	</image>
	<image>
		<date>05.13.05</date>
		<title>SD wedding</title>
		<desc>Wedding at SD convention center</desc>
		<thumb>wedding5.jpg</thumb>
		<img>wedding5.jpg</img>
	</image>
	<image>
		<date>11.21.05</date>
		<title>Wedding Time</title>
		<desc>Wedding photo</desc>
		<thumb>wedding6.jpg</thumb>
		<img>wedding6.jpg</img>
	</image>
	<image>
		<date>06.14.06</date>
		<title>Togetherness</title>
		<desc>Wedding ceremony in Hawaii</desc>
		<thumb>wedding7.jpg</thumb>
		<img>wedding7.jpg</img>
	</image>
</category>
<category name="View of Asia">
	<image>
		<date>04.01.06</date>
		<title>Bangkok</title>
		<desc>Bankok</desc>
		<thumb>bangkok1.jpg</thumb>
		<img>bangkok1.jpg</img>
	</image>
	<image>
		<date>04.01.06</date>
		<title>Bangkok</title>
		<desc>Bankok</desc>
		<thumb>bangkok2.jpg</thumb>
		<img>bangkok2.jpg</img>
	</image>
	<image>
		<date>04.01.06</date>
		<title>shopping in Singapore</title>
		<desc>shopping in Singapore Lau Pa Sat Festival Market Great place to drink & eat your lobsters..</desc>
		<thumb>sin.jpg</thumb>
		<img>sin.jpg</img>
	</image>
	<image>
		<date>04.01.06</date>
		<title>China Town Mexico</title>
		<desc>Chinese lion head over drums</desc>
		<thumb>china1.jpg</thumb>
		<img>china1.jpg</img>
	</image>
	<image>
		<date>04.05.06</date>
		<title>little bells</title>
		<desc>voice from the thailand's temple</desc>
		<thumb>bell.jpg</thumb>
		<img>bell.jpg</img>
	</image>
	<image>
		<date>04.08.06</date>
		<title>Green tea farm</title>
		<desc>Daehan green tea farm: Boseong, South Korea</desc>
		<thumb>korea1.jpg</thumb>
		<img>korea1.jpg</img>
	</image>
</category>
<category name="Trip to Montreal">
	<image>
		<date>03.25.06</date>
		<title>Touring Old Montreal</title>
		<desc>**** Touring Old Montreal ****</desc>
		<thumb>tour3.jpg</thumb>
		<img>tour3.jpg</img>
	</image>
	<image>
		<date>03.25.06</date>
		<title>Touring Old Montreal</title>
		<desc>**** Touring Old Montreal ****</desc>
		<thumb>tour6.jpg</thumb>
		<img>tour6.jpg</img>
	</image>
	<image>
		<date>03.25.06</date>
		<title>Touring Old Montreal</title>
		<desc>**** Touring Old Montreal ****</desc>
		<thumb>tour10.jpg</thumb>
		<img>tour10.jpg</img>
	</image>
	<image>
		<date>03.25.06</date>
		<title>Touring Old Montreal</title>
		<desc>**** Touring Old Montreal ****</desc>
		<thumb>tour11.jpg</thumb>
		<img>tour11.jpg</img>
	</image>
	<image>
		<date>03.25.06</date>
		<title>Touring Old Montreal</title>
		<desc>**** Touring Old Montreal ****</desc>
		<thumb>tour13.jpg</thumb>
		<img>tour13.jpg</img>
	</image>
</category>
</gallery>

 

My code for generating the XML file:

 

<?php
// Include the database connection
include("admin/inc/db-connection.php");

$q = "SELECT * FROM `mftl_categories`";
$r = mysql_query($q);

$file = "gallery_test.xml";

$fh = fopen($file, 'w');
$stringData = "<gallery title=\"Miracle Lift - Before And After Photos\" thumbDir=\"./images/thumbs/\" imageDir=\"./images/\" random=\"true\">\n";
$stringData .= "<category name=\"$catName\">\n";
fwrite($fh, $stringData);

// Loop
while ($a = mysql_fetch_array($r))
{

  $catName = $a['category_name'];
  
  // Select the images...
  $qI = "SELECT * FROM `mftl_images` WHERE `image_category`='$catName'";
  $rI = mysql_query($qI);
  $aI = mysql_fetch_array($rI);
  
  // Local vars
  $imgDate = $aI['date'];
  $imgFull = $aI['image_fullsize']; 
  $imgThum = $aI['image_thumbnail']; 
  $imgDesc = $aI['image_description']; 
  
  $stringData .= "  <image>\n";
$stringData .= "   <date>$imgDate</date>\n";
$stringData .= "   <title>TEXT</title>\n";
$stringData .= "   <desc>$imgDesc</desc>\n";
$stringData .= "   <thumb>$imgThum</thumb>\n";
$stringData .= "   <img>$imgFull</img>\n";  
  $stringData .= "  </image>\n";  
  $stringData .= "</category>\n";
  
}

fwrite($fh, $stringData);

$stringData .= "</gallery>\n";
fwrite($fh, $stringData);

fclose($fh);

?>

 

this produces something very close but not quite right:

 

<gallery title="Miracle Lift - Before And After Photos" thumbDir="./images/thumbs/" imageDir="./images/" random="true">
<gallery title="Miracle Lift - Before And After Photos" thumbDir="./images/thumbs/" imageDir="./images/" random="true">
<category name="Before Miracle Lift">
  <image>
   <date>2010-01-02 14:06:32</date>
   <title>TEXT</title>
   <desc>Esther, before her procedure.</desc>
   <thumb>thumb-esther_before_(front).jpg</thumb>
   <img>esther_before_(front).jpg</img>
  </image>
</category>
<category name="After Miracle Lift">
  <image>
   <date>2010-01-02 14:07:07</date>
   <title>TEXT</title>
   <desc>Susan 4 days after.</desc>
   <thumb>thumb-sus_4_days_after_(front).jpg</thumb>
   <img>sus_4_days_after_(front).jpg</img>
  </image>
</category>
<gallery title="Miracle Lift - Before And After Photos" thumbDir="./images/thumbs/" imageDir="./images/" random="true">
<category name="Before Miracle Lift">
  <image>
   <date>2010-01-02 14:06:32</date>
   <title>TEXT</title>
   <desc>Esther, before her procedure.</desc>
   <thumb>thumb-esther_before_(front).jpg</thumb>
   <img>esther_before_(front).jpg</img>
  </image>
</category>
<category name="After Miracle Lift">
  <image>
   <date>2010-01-02 14:07:07</date>
   <title>TEXT</title>
   <desc>Susan 4 days after.</desc>
   <thumb>thumb-sus_4_days_after_(front).jpg</thumb>
   <img>sus_4_days_after_(front).jpg</img>
  </image>
</category>
</gallery>

 

i can't see why the title (top) tag is generated twice, the loop doesn't start untill well below,.

 

thanks fior any help guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/186934-on-the-fly-xml-generation/
Share on other sites

Hi Mchl,

 

ah i never saw that, thanks mate :) i use xmlWriter a fair bit in vbnet, the output using fwrite has been surprisibly good, the output now is:

 

<gallery title="Miracle Lift - Before And After Photos" thumbDir="./images/thumbs/" imageDir="./images/" random="true">
<category name="Before Miracle Lift">
  <image>
   <date>2010-01-02 14:06:32</date>
   <title>TEXT</title>
   <desc>Esther, before her procedure.</desc>
   <thumb>thumb-esther_before_(front).jpg</thumb>
   <img>esther_before_(front).jpg</img>
  </image>
</category>
<category name="After Miracle Lift">
  <image>
   <date>2010-01-02 14:07:07</date>
   <title>TEXT</title>
   <desc>Susan 4 days after.</desc>
   <thumb>thumb-sus_4_days_after_(front).jpg</thumb>
   <img>sus_4_days_after_(front).jpg</img>
  </image>
</category>
</gallery>

 

Using the ammended code:

 

<?php
// Include the database connection
include("admin/inc/db-connection.php");

$q = "SELECT * FROM `mftl_categories`";
$r = mysql_query($q);

$file = "gallery_test.xml";

$fh = fopen($file, 'w');
$stringData = "<gallery title=\"Miracle Lift - Before And After Photos\" thumbDir=\"./images/thumbs/\" imageDir=\"./images/\" random=\"true\">\n";
//$stringData .= "<category name=\"$catName\">\n";
//fwrite($fh, $stringData);

// Loop
while ($a = mysql_fetch_array($r))
{

  $catName = $a['category_name'];
  
  // Select the images...
  $qI = "SELECT * FROM `mftl_images` WHERE `image_category`='$catName'";
  $rI = mysql_query($qI);
  $aI = mysql_fetch_array($rI);
  
  // Local vars
  $imgDate = $aI['date'];
  $imgCate = $aI['image_category'];
  $imgFull = $aI['image_fullsize']; 
  $imgThum = $aI['image_thumbnail']; 
  $imgDesc = $aI['image_description']; 
  
  if ($catName == $imgCate)
  {
    $stringData .= "<category name=\"$catName\">\n";
  }
  
  $stringData .= "  <image>\n";
$stringData .= "   <date>$imgDate</date>\n";
$stringData .= "   <title>TEXT</title>\n";
$stringData .= "   <desc>$imgDesc</desc>\n";
$stringData .= "   <thumb>$imgThum</thumb>\n";
$stringData .= "   <img>$imgFull</img>\n";  
  $stringData .= "  </image>\n";  
  $stringData .= "</category>\n";
  
}

//fwrite($fh, $stringData);

$stringData .= "</gallery>\n";
fwrite($fh, $stringData);

fclose($fh);

?>

 

The only thing is, there is actually 3 images being pulled from the database, but the way the code is now, it's only showing 1 of each category, i ideally need it structured like:

 

<gallery title="Miracle Lift - Before And After Photos" thumbDir="./images/thumbs/" imageDir="./images/" random="true">
<category name="Before Miracle Lift">
  <image>
   <date>2010-01-02 14:06:32</date>
   <title>TEXT</title>
   <desc>Esther, before her procedure.</desc>
   <thumb>thumb-esther_before_(front).jpg</thumb>
   <img>esther_before_(front).jpg</img>
  </image>
  <image>
   <date>2010-01-02 14:06:32</date>
   <title>TEXT</title>
   <desc>April, before her procedure.</desc>
   <thumb>thumb-april_before_(front).jpg</thumb>
   <img>april_before_(front).jpg</img>
  </image>
</category>
<category name="After Miracle Lift">
  <image>
   <date>2010-01-02 14:07:07</date>
   <title>TEXT</title>
   <desc>Susan 4 days after.</desc>
   <thumb>thumb-sus_4_days_after_(front).jpg</thumb>
   <img>sus_4_days_after_(front).jpg</img>
  </image>
</category>
</gallery>

 

is there any way i could accomplish this? i can't think how i would go about it lol

 

thanks mate

 

Graham

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.