Jump to content

save xml file php code?


jaiffed

Recommended Posts

I have this code

 

header("Content-type: text/xml");

$database = "rprice";

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

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

 

$query = "SELECT * FROM plist ORDER BY date DESC";

$resultID = mysql_query($query, $linkID) or die("Data not found.");

 

$xml_output = "<?xml version=\"1.0\"?>\n";

$xml_output .= "<graph>\n";

 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){

    $row = mysql_fetch_assoc($resultID);

    $xml_output .= "\t<set date =\"" . $row['date'] . "\" value =\"" . $row['sprice'] . "\">\n";

    $xml_output .= "\t</set>\n"; 

}

$xml_output .= "</graph>";

 

echo $xml_output;

 

 

 

I want to save this xml file into the folder name Data what is the code of that Plz help

Link to comment
Share on other sites

Use SimpleXML. It was built for this.

 

Comments explain what I'm doing.

 

<?php

// Set up our headers
header( 'Content-type: text/xml; charset=UTF-8' ); 

// Bare bones XML to generate from.
$xml_str = '<?xml version="1.0" ?><users></users>';

// Create a new instance of SimpleXML using the bones
$xml = new SimpleXMLElement( $xml_str );
// Connect to database
$db = new MySQLi( 'localhost','root','','db' );

// Grab all the users and the data we need
$q = 'SELECT `id`,`name`,`email`,`realname`
FROM `users`';
// Perform Query
$r = $db->query( $q );
// Check if query failed. If so, echo empty XML file. Should log error.
if( $r === FALSE ) die( $xml->asXML() );

// Loop through results
while( $row = $r->fetch_assoc() ) {
// Create a new child in '<users>' called '<user>'. Save this to
// variable so we can give it arrtibutes and children
$user = $xml->addChild( 'user' );
// Set attribute - '<user id="$id">'
$user->addAttribute( 'id', $row['id'] );
// Give '<user>' children '<user id="$id"><name>$name</name></user>' etc.
$user->addChild( 'name', $row['name'] );
$user->addChild( 'email', $row['email'] );
$user->addChild( 'realname', $row['realname'] ); 
}

// Free up MySQL results
$r->free();

// Echo our finished XML file.
echo $xml->asXML();

?>

Link to comment
Share on other sites

i want xml file in ths format

<graph>

<set name='Jan' value='17400' hoverText='January'/>

</graph>

 

It's very easy to change my code to do that exactly.

 

im having error in this

$db = new MySQLi( 'localhost','root','12345','rlist' );

 

Then you've entered incorrect database information. That's my guess, without seeing the error.

Link to comment
Share on other sites

Are you trying to write to a database or a file?  If file try this.

Try this after your echo statement.

$fhandle = fopen("data.xml", 'w') or die("can't  create file");
fwrite($fhandle, $xml_output) or die("can't write to the file");
fclose($fhandle)

This should write your date to the file data.xml.

Link to comment
Share on other sites

everything is done after using possien code at the end the script

 

$fhandle = fopen("data.xml", 'w') or die("can't  create file");

fwrite($fhandle, $xml_output) or die("can't write to the file");

fclose($fhandle)

?>

 

Its work but after that how could i open html file i put include 'dream.html'; & some other codes but its not working help me again guys

Link to comment
Share on other sites

If you want to do it in php you might try this.  Take the dream.html page and rename it to dream.php.  In the page at the beginning create a variable say $dream and put all of the html in that variable.

$dream= 'all of your html goes here.....';

In your main page use :

require_once('dream.php');
echo $dream;

Link to comment
Share on other sites

I have an dream.html file so i have to know how can i open this file after the end of this php file which create xml file.

 

<?php

 

// Set up our headers

header( 'Content-type: text/xml; charset=UTF-8' );

 

// Bare bones XML to generate from.

$xml_str = '<?xml version="1.0" ?><users></users>';

 

// Create a new instance of SimpleXML using the bones

$xml = new SimpleXMLElement( $xml_str );

// Connect to database

$db = new MySQLi( 'localhost','root','','db' );

 

// Grab all the users and the data we need

$q = 'SELECT `id`,`name`,`email`,`realname`

FROM `users`';

// Perform Query

$r = $db->query( $q );

// Check if query failed. If so, echo empty XML file. Should log error.

if( $r === FALSE ) die( $xml->asXML() );

 

// Loop through results

while( $row = $r->fetch_assoc() ) {

// Create a new child in '<users>' called '<user>'. Save this to

// variable so we can give it arrtibutes and children

$user = $xml->addChild( 'user' );

// Set attribute - '<user id="$id">'

$user->addAttribute( 'id', $row['id'] );

// Give '<user>' children '<user id="$id"><name>$name</name></user>' etc.

$user->addChild( 'name', $row['name'] );

$user->addChild( 'email', $row['email'] );

$user->addChild( 'realname', $row['realname'] );

}

 

// Free up MySQL results

$r->free();

 

// Echo our finished XML file.

echo $xml->asXML();

$fhandle = fopen("data.xml", 'w') or die("can't  create file");

fwrite($fhandle, $xml_output) or die("can't write to the file");

fclose($fhandle);

?>

 

this file create the .xml file & save after that processing i want to open a html file which is dream.html i used

include ' dream.html';

after the

fclose($fhandle);

but its not working getting errror

 

Link to comment
Share on other sites

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.