PHP to XML and write a .xml file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BygMony
Forum Newbie
Posts: 2
Joined: Mon Feb 26, 2007 9:24 am

PHP to XML and write a .xml file

Post by BygMony »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok I've been working on this for two weeks. I am using PHP/MySql to store info for a XML formatted doc. I used the technique from http://www.gotoandlearn.com to produce XML with PHP. I added the ability to write the XML file that show up in IE and that works great. I recently made a change to have keywords in a separate DB because I need to have the keywords inputted separately in the XML. This XML file is used for SCORM compliancy based on the ADL website https://adlregistry.dtic.mil/ (https://adlregistry.dtic.mil/6/index2_7.htm).

The problem now is that when I run the script to produce the XML page via IE and write it into the xml folder it prints the keyword in IE but it will not write them in the .xml file. It writes all the other data but just shows the do while loop i have. Below is the code. To me it appears that the variable is not being reconized in the bottome write file portion of the code. This was done with Dreamweaver and Handcoding and reworked code off the internet. The page is intitated by clicking on a link that displays the course title and takes you to a page that displays all the course info in XML format.

Thanks
BygMony

Code: Select all

<?php require_once('../Connections/scorm.php'); ?>
<?php
$colname_rsScorm = "-1";
if (isset($_GET['SCO_Description'])) {
  $colname_rsScorm = (get_magic_quotes_gpc()) ? $_GET['SCO_Description'] : addslashes($_GET['SCO_Description']);
}
$colname2_rsScorm2 = "-1";
if (isset($_GET['SCO_Description'])) {
  $colname2_rsScorm2 = (get_magic_quotes_gpc()) ? $_GET['SCO_Description'] : addslashes($_GET['SCO_Description']);
}
mysql_select_db($database_scorm, $scorm);
$query_rsScorm = sprintf("SELECT * FROM sco_metadata WHERE COI_Number = %s", $colname_rsScorm);
$rsScorm = mysql_query($query_rsScorm, $scorm) or die(mysql_error());
$row_rsScorm = mysql_fetch_assoc($rsScorm);
$totalRows_rsScorm = mysql_num_rows($rsScorm);

mysql_select_db($database_scorm, $scorm);
$query_rsScorm2 = sprintf("SELECT * FROM keywords WHERE COI_Number = %s", $colname2_rsScorm2);
$rsScorm2 = mysql_query($query_rsScorm2, $scorm) or die(mysql_error());
$row_rsScorm2 = mysql_fetch_assoc($rsScorm2);
$totalRows_rsScorm2 = mysql_num_rows($rsScorm2);

$results2 = mysql_query($query_rsScorm2);
$line2 = mysql_fetch_assoc($results2);


$results = mysql_query($query_rsScorm);
$line = mysql_fetch_assoc($results);

?>

<?php /* $line["database field"] must make upper/lower case */


echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";

echo "<registryTransaction xmls=\"http://hdl.cordra.net/2000.2.1/ADL-R-Reg-T\" xmlns:lom=\"http://Itsc.ieee.org/xsd/LOM\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://Itsc.ieee.org/xsd/LOMhttp://hdl.cordra.net/2000.2/adlreg-lom?VData=version&". "id=1http://hdl.cordra.net/2000.2.1/ADL-R-Reg-Thttp://hdl.cordra.net/2000.2.1/ADL-R-Reg-T?VData=version&=1\">\n";

echo "<course_metadata>\n";


echo "<metadata>\n";

      
   
   echo "<mod_title>" . $line["Module_Title"] . "</mod_title>\n";
   echo  "<lesson_title>" . $line["Lesson_Title"] . "</lesson_title>\n";
   echo   "<coi_num>" . $line["COI_Number"]. "</coi_num>\n";
   echo    "<version>" . $line["Version"] . "</version>\n";
   
   //work great, loops and displays all keywords from keyword database
   do {
   echo   "<lom:keyword>\n";
   echo   "<lom:string language=\"en-US\">" . $line2["Keywords"] . "</lom:string>\n";
   echo    "</lom:keyword>\n";
   }
   while($line2 = mysql_fetch_assoc($results2));
   echo      "<status>" . $line["Status"] ."</status>\n";
   echo       "<sco_description>" . $line["SCO_Description"] . "</sco_description>\n";
   echo        "<date_created>" . $line["Date_Created"] . "</date_created>\n";
   echo         "<security_classification>" . $line["Security_Classification"] . "</security_classification>\n";
   echo          "<format>" . $line["Format"] . "</format>\n";
   echo         "<document>" . $line["Document_Handler"] . "</document>\n";
   echo        "<interactivity_level>" . $line["Interactivity_Level"] . "</interactivity_level>\n";
   echo       "<learning_resource_type>" . $line["Learning_Resource_Type"] . "</learning_resource_type>\n";
   echo      "<contribute>" . $line["Contribute"] . "</contribute>\n";
   echo     "<copyright>" . $line["Copyright_restrictions"] . "</copyright>\n";
   echo    "<location>" . $line["Location"] . "</location>\n";
   echo   "<aggregation_level>" . $line["Aggregation_Level"] . "</aggregation_level>\n";
   echo  "<knowledge_type>" . $line["Knowledge_Type"] . "</knowledge_type>\n";
   echo "<terminal_learning_obj>" . $line["Terminal_Learning_Obj"] . "</terminal_learning_obj>\n";


echo "</metadata>\n";


   
echo "</course_metadata>\n";
echo "</registryTransaction>\n";

//Write .xml file to folder

$myFile = str_replace(' ','',$line["Module_Title"]). '.xml';
$fh = fopen("xml/".$myFile, 'w') or die("can't open file");
$stringData = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
<registryTransaction xmls=\"http://hdl.cordra.net/2000.2.1/ADL-R-Reg-T\" xmlns:lom=\"http://Itsc.ieee.org/xsd/LOM\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://Itsc.ieee.org/xsd/LOMhttp://hdl.cordra.net/2000.2/adlreg-lom?VData=version&id=1http://hdl.cordra.net/2000.2.1/ADL-R-Reg-Thttp://hdl.cordra.net/2000.2.1/ADL-R-Reg-T?VData=version&=1\">

<course_metadata>
<metadata>
<mod_title>" . $line["Module_Title"] . "</mod_title>
<lesson_title>" . $line["Lesson_Title"] . "</lesson_title>
<coi_num>" . $line["COI_Number"] . "</coi_num>
<version>" . $line["Version"] . "</version>
<!--This will not loop and it prints the loop as you see it.-->
do
{
<lom:keyword>
<lom:string language=\"en-US\">" . $line2["Keywords"] . "</lom:string>
</lom:keyword>
 }
   while($line2 = mysql_fetch_assoc($results2));<!--It calls $results2 resource id #6 and deletes $line2. Everything else prints out to a text file correctly-->
<status>" . $line["Status"] ."</status>
<sco_description>" . $line["SCO_Description"] . "</sco_description>
<date_created>" . $line["Date_Created"] . "</date_created>
<security_classification>" . $line["Security_Classification"] . "</security_classification>
<format>" . $line["Format"] . "</format>
<document>" . $line["Document_Handler"] . "</document>
<interactivity_level>" . $line["Interactivity_Level"] . "</interactivity_level>
<learning_resource_type>" . $line["Learning_Resource_Type"] . "</learning_resource_type>
<contribute>" . $line["Contribute"] . "</contribute>
<copyright>" . $line["Copyright_restrictions"] . "</copyright>
<location>" . $line["Location"] . "</location>
<aggregation_level>" . $line["Aggregation_Level"] . "</aggregation_level>
<knowledge_type>" . $line["Knowledge_Type"] . "</knowledge_type>
<terminal_learning_obj>" . $line["Terminal_Learning_Obj"] . "</terminal_learning_obj>
</metadata>
</course_metadata>
</registryTransaction>\n";
fwrite($fh, $stringData);
fclose($fh);


mysql_free_result($rsScorm);

mysql_free_result($rsScorm2);
?>
XML FILE THAT GETS WRITTEN

Code: Select all

<?xml version="1.0" encoding="utf-8" ?>
<registryTransaction xmls="http://hdl.cordra.net/2000.2.1/ADL-R-Reg-T" xmlns:lom="http://Itsc.ieee.org/xsd/LOM" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://Itsc.ieee.org/xsd/LOMhttp://hdl.cordra.net/2000.2/adlreg-lom?VData=version&id=1http://hdl.cordra.net/2000.2.1/ADL-R-Reg-Thttp://hdl.cordra.net/2000.2.1/ADL-R-Reg-T?VData=version&=1">

<course_metadata>
<metadata>
<mod_title>Griffin</mod_title>
<lesson_title>BygMony</lesson_title>
<coi_num>990923</coi_num>
<version>1</version>
<!--This will not loop and it prints the loop as you see it.-->
do
{
<lom:keyword>
<lom:string language="en-US"></lom:string>
</lom:keyword>
 }
   while( = mysql_fetch_assoc(Resource id #6));<!--It calls Resource id #6 resource id #6 and deletes . Everything else prints out to a text file correctly-->
<status>Green</status>
<sco_description>None.</sco_description>
<date_created>Nov 29, 2005</date_created>
<security_classification>Classified</security_classification>
<format>full</format>
<document>FOUO</document>
<interactivity_level>Category 1</interactivity_level>
<learning_resource_type>Web based</learning_resource_type>
<contribute>none</contribute>
<copyright>none</copyright>
<location>www.jfcom.mil</location>
<aggregation_level>6</aggregation_level>
<knowledge_type>Process</knowledge_type>
<terminal_learning_obj>none.</terminal_learning_obj>
</metadata>
</course_metadata>
</registryTransaction>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

have you considered:

using one of PHP's xml libraries to build, modify and/or validate the XML
using code tags on this forum so we'll be able to read your code?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The code provided has a do..while() in the middle of a string. That's why it's shown and why it doesn't run.

Also, if $query_rsScorm2 or $query_rsScorm find zero results, this script will break.. not to mention the SQL injection potential for $colname_rsScorm and $colname_rsScorm2.

mysql_real_escape_string() should pretty much always be used. If get_magic_quotes_gpc() returns true, you should actually stripslashes() the data then allow it to fall through the mysql_real_escape_string() call.
BygMony
Forum Newbie
Posts: 2
Joined: Mon Feb 26, 2007 9:24 am

Will post the right way next time

Post by BygMony »

Also even if I don't have the do while loop the $line2["Keywords"] still will not print. Thank you for answering back.

BygMony
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The variable will not be set at that point.
Post Reply