Jump to content

Generating XML Files

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
chad

chad

    Learning Programmer

  • Members
  • PipPipPip
  • 68 posts
Hi guys, basically im trying to generate an xml file for my flash in my project. Here is the code to generate one.


$dom = new DOMDocument("1.0");


$root = $dom->createElement("thumbnails");

$dom->appendChild($root);

$dom->formatOutput=true;


$item = $dom->createElement("thumbnail");

$root->appendChild($item);


$text = $dom->createTextNode("pepperoni");

$item->appendChild($text);


$price = $dom->createAttribute("price");

$item->appendChild($price);


$priceValue = $dom->createTextNode("4");

$price->appendChild($priceValue);


$cdata = $dom->createCDATASection("\nCustomer requests that pizza be sliced into 16 square pieces\n");

$root->appendChild($cdata);


$dom->save("order.xml");


$order = $dom->save("order.xml");


the output would be like this


<?xml version="1.0"?>

<thumbnails><thumbnail price="4">pepperoni</thumbnail><![CDATA[

Customer requests that pizza be sliced into 16 square pieces

]]></thumbnails>


Now what i want is to have a loop of that one. Display not just one but multiple ones. And i want to have the arrangement like this.


<?xml version="1.0" encoding="utf-8"?>

<thumbnails>


<thumbnail id="0" thumbs_url="thumb/test.jpg" image_url="test.jpg" title="Test" link="http://www.google.com">

<![CDATA[Test

]]>

</thumbnail>


</thumbnails>


Any suggestions on how to do it?

#2
chad

chad

    Learning Programmer

  • Members
  • PipPipPip
  • 68 posts
seems like no one has been able to answer my question
anyway i have already found a solution to it.

here it is
hope it helps you guys in the future


$command = "rm -r /your/directory/*"; 


system($command);  


$host = "localhost"; 

$user = "test"; 

$pass = "test"; 

$database = "test"; 


$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 

mysql_select_db($database, $linkID) or die("Could not find database."); 


// create doctype

$dom = new DOMDocument("1.0");


// create root element

$root = $dom->createElement("thumbnails");

$dom->appendChild($root);

$dom->formatOutput=true;


$a = mysql_query("SELECT * FROM test WHERE featured = 1");

$b = mysql_num_rows($a);


for($i = 0; $i < $b; $i++)

{

	$c = mysql_fetch_array($a);

	

	$c['description'] = str_replace("&", "&", $c['description']); 

    $c['description'] = str_replace("<", "<", $c['description']); 

    $c['description'] = str_replace(">", ">", $c['description']); 

    $c['description'] = str_replace("\"", """, $c['description']);

	$c['description'] = str_replace("'", "’", $c['description']);

	$c['title'] = strtoupper($c['title']);

			

	// create child element

	$item = $dom->createElement("thumbnail");

	$root->appendChild($item);

	

	// create CDATA section

	$cdata = $dom->createCDATASection("\n".$c['description']."\n");

	$item->appendChild($cdata);

	

	// create attribute node

	$id = $dom->createAttribute("id");

	$item->appendChild($id);

	

	$thumbs_url = $dom->createAttribute("thumbs_url");

	$item->appendChild($thumbs_url);

	

	$image_url = $dom->createAttribute("image_url");

	$item->appendChild($image_url);

	

	$title = $dom->createAttribute("title");

	$item->appendChild($title);

	

	// create attribute value node

	$idValue = $dom->createTextNode("".$c['id']."");

	$id->appendChild($idValue);

	

	$thumbs_urlValue = $dom->createTextNode("".$c['attachment']."");

	$thumbs_url->appendChild($thumbs_urlValue);

	

	$image_urlValue = $dom->createTextNode("".$c['attachment']."");

	$image_url->appendChild($image_urlValue);

	

	$titleValue = $dom->createTextNode("".$c['title']."");

	$title->appendChild($titleValue);

}


// save tree to file

$dom->save("thumbnails.xml");


// save tree to string

$order = $dom->save("thumbnails.xml");