View Full Version : How to retrieve the data from PHP to XML?
NewCarol
03-04-2009, 01:45 PM
Hello everyone,
Im new to PHP, however I do have some experience with XML.
I need to create XML file where the data will be retrieved from the PHP. What is the best way to do it?:confused:
For example:
<listOfSpecies>
<species id="N1" name="ATP" />
<species id="N2" name="ADP"/>
<species id="N3" name="H"/>
</listOfSpecies>
(IDs and names has been retrieved from the php)
Thanks. NewCarol
JasonDFR
03-04-2009, 02:43 PM
EDIT:
I think I read your post too quickly. You want to CREATE the XML file? Or do already have the XML file and you need to get information out of it?
If you want to create an XML file, let me know how you are getting your data. From a database? Post an example of the data you have.
END EDIT.
I am glad there have been some questions about XML on here lately. I am getting a lot of practice with simpleXML.
species.xml
<?xml version="1.0" ?>
<listOfSpecies>
<species id="N1" name="ATP" />
<species id="N2" name="ADP"/>
<species id="N3" name="H"/>
</listOfSpecies>
yourfile.php
<?php
$xml = simplexml_load_file('species.xml');
foreach ( $xml as $species ) {
echo 'ID: ' . $species->attributes()->id . '<br />';
echo 'Name: ' . $species->attributes()->name . '<br />';
}
?>
JasonDFR
03-04-2009, 03:20 PM
This will create a XML file:
<?php
// $rows is just sample data
// You'll have to gather up your data here:
$rows = array();
$rows[] = array( 'id' => 'N1', 'name' => 'ATP');
$rows[] = array( 'id' => 'N2', 'name' => 'ADP');
$rows[] = array( 'id' => 'N3', 'name' => 'H');
$file= fopen("list_of_species.xml", "w");
$xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\r\n";
$xml .= "<listOfSpecies>\r\n";
foreach ($rows as $row) {
$xml .= '<species id="' . $row['id'] . '" name="' . $row['name'] . '" />' . "\r\n";
}
$xml .= "</listOfSpecies>";
fwrite($file, $xml);
fclose($file);
NewCarol
03-04-2009, 03:26 PM
JasonDFR,
Thanks a lot for your reply.
I do have the xml file. Im using php to access the database. The outcome has to be in xml format.
The example that I put in my first post - is the actual outcome (which im trying to reproduce)
(ps: i can send you example of the data by email)
NewCarol
JasonDFR
03-04-2009, 03:36 PM
You are saying you have the XML file? That is what confused me. I think you want to create an XML file, right?
If so, my second post should work then. It starts with nothing and builds an XML file.
Look at my second post.
Query your DB where I have the $rows array, get the $result, then in the code below, replace:
// replace this with your db specific code
foreach ($rows as $row) {
$xml .= '<species id="' . $row['id'] . '" name="' . $row['name'] . '" />' . "\r\n";
}
depending on your database, with something like:
while ( $row = mysqli_fetch_array($result) ) {
$xml .= '<species id="' . $row['id'] . '" name="' . $row['name'] . '" />' . "\r\n"
}
Now, if you want to add to an existing XML file, it gets a bit more complicated. It is probably best to create a new XML file everytime you need it. I guess there are some exceptions, and it is possible to manipulate an XML document, but since you have a database already, I would just make a new xml file each time.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.