Jump to content

Recommended Posts

Hi Everyone,

 

I'm attempting to create an xml file using php, pulling values from mysql. The code below is a slightly modified version of code I have that works perfectly for creating an rss feed. My problem is now that I can't get it to work for just plain xml. I would like this script to write an xml file and store it on the server. For some reason that I can't figure out I get a "No element found" error on the xml and of course no file is written. Any help would be appreciated as I'm past my deadline on this and have no hope in sight. here's the code I'm using for the xml creation:

 

Index.php

 

<?
header("Content-Type: application/xml; charset=ISO-8859-1");
include("classes/xml.class.php");
$xml = new XML();
echo $xml->GetFeed();
?>

xml.class.php

 

<?
class XML
{
public function XML()
{
	require_once ('classes/mysql_connect.php');
}

public function GetFeed()
{
	return $this->getItems();
}

private function dbConnect()
{
	DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
}

private function getItems()
{
	$itemsTable = "Profiles";
	$mediaTable = "media";
	$this->dbConnect($itemsTable, $mediaTable);
	$query = "SELECT * FROM $itemsTable, $mediaTable WHERE $itemsTable.ID = $mediaTable.med_prof_id LIMIT 1,30";
	$result = mysql_db_query (DB_NAME, $query, LINK);
	$items = '';

	while($row = mysql_fetch_array($result))
	{
		$text = $row['ListingDescription'];
		$title = $row['ListingTitle'];
	    $title2 = preg_replace(array('/</', '/>/', '/"/', '/&/'), array('<', '>', '"', '&'), $title);
		$text2 = preg_replace(array('/</', '/>/', '/"/', '/&/'), array('<', '>', '"', '&'), $text);			
		$img= $row['med_file'];
			$items .= '<listings>
					 <title>'. $title2 .'</title>
					  <link>'. "http://www.example.com/".$row['ID'] .'</link>
					 <description>'. $text2 .'</description>
					 <img>'. $img .'</img>
				 </listings>';
	}
}

	private function writeItems()
	{
	$filenamepath .=   "images.xml";

$fp = fopen($filenamepath,'w');

            $write = fwrite($fp,$items);
}
}

?>

 

The original code as an rss creator that I modified. Again, this works just fine for creating rss feeds:

 

 

index.php

<?
header("Content-Type: application/rss+xml; charset=ISO-8859-1");
include("classes/RSS.class.php");
$rss = new RSS();
echo $rss->GetFeed();
?>

 

rss.class.php

 

<?
class RSS
{
public function RSS()
{
	require_once ('classes/mysql_connect.php');
}

public function GetFeed()
{
	return $this->getDetails() . $this->getItems();
}

private function dbConnect()
{
	DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
}

private function getDetails()
{
		$details = '<?xml version="1.0" encoding="ISO-8859-1" ?>
				<rss version="2.0">
					<channel>
						<title>'. 'example.com' .'</title>
						<link>'. 'http://www.example.com' .'</link>
						<description>'. 'example.com' .'</description>
						<image>
    						<url>'. 'http://www.example.com' .'</url>
    						<title>'. 'example.com' .'</title>
    						<link>'. 'example.com/' .'</link>
					 </image>

						';
		return $details;
}


private function getItems()
{
	$itemsTable = "Profiles";
	$this->dbConnect($itemsTable);
	$query = "SELECT * FROM ". $itemsTable;
	$result = mysql_db_query (DB_NAME, $query, LINK);
	$items = '';

	while($row = mysql_fetch_array($result))
	{
		$text = $row['ListingDescription'];
		$title = $row['ListingTitle'];
	    $title2 = preg_replace(array('/</', '/>/', '/"/', '/&/'), array('<', '>', '"', '&'), $title);
		$text2 = preg_replace(array('/</', '/>/', '/"/', '/&/'), array('<', '>', '"', '&'), $text);			
			$items .= '<item>
					 <title>'. $title2 .'</title>
					 <link>'. "http://www.example.com/".$row['ID'] .'</link>
					 <description>'. $text2 .'</description>
				 </item>';
	}
	$items .= '</channel>
			 </rss>';
	return $items;
}

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/155517-write-from-mysql-to-xml-file/
Share on other sites

ok, just tried it. I must have misunderstood you. here's what I did at the end of xml.class.php:

 

public function writeItems()
	{
	$filenamepath .=   "datafeed.xml";

$fp = fopen($filenamepath,'w');

          $write = fwrite($fp,$items);
		$write = fwrite($fp,$this->GetFeed());
}
}

?>

 

and in index.php

 

<?
header("Content-Type: application/xml; charset=ISO-8859-1");
include("classes/xml.class.php");
$xml = new XML();
$xml->writeItems();
echo $xml->GetFeed();
?>

 

The above did not work. I still get a no element error and no file is written. Did I misunderstand the placement of your code?

Btw, this is going to be a huge xml, over 7000 entries. thats why I didn't use something like SimpleXML. Don't know if that matters here, I assumed I would be ok as the rss feed is the same information.

 

Thanks much, let me know what you think.

Michael

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.