Hello friends
I am working with xml and php and mysql…in this code i am inserting downloaded xml file into database…but the problem is with my xml file…eg…so plz help me…
<<<<XML FILE ON WHICH I HAVE TO WORK>>>>>
<root>
<patient>
<int_patient_id>56</int_patient_id>
<int_login_id>23</int_login_id>
<str_patient_salutation>8</str_patient_salutation>
<patient_insurance_plan>
<int_patient_insurance_plan_id>3</int_patient_insurance_plan_id>
<int_patient_login_id>2</<int_patient_login_id>
</patient_insurance_plan>
</patient>
</root>
but the code works only this type of file
<<<CODE WORKS ONLY FOR THIS FILE>>>
<patient>
<int_patient_id>56</int_patient_id>
<int_login_id>23</int_login_id>
<str_patient_salutation>8</str_patient_salutation>
</patient>
<patient_insurance_plan>
<int_patient_insurance_plan_id>3</int_patient_insurance_plan_id>
<int_patient_login_id>2</<int_patient_login_id>
</patient_insurance_plan>
</root>
<<<<WHOLE FILE CODE DOWN HERE>>>>
<?php
require_once "./PEAR/DB.php";
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
ini_set("max_execution_time",1000);
$strNewFileName = "xmlArrayTest1.xml";
if(!move_uploaded_file($_FILES["file"]["tmp_name"],'./'.$strNewFileName))
{
$MYSQL_ERROR = "Error in moving the xml file";
return 0;
}
class patientInfo
{
var $insidePatient = false;
var $insidePatientInsurancePlan = false;
var $link, $tag, $strTableName, $strNewFileName, $strFieldName, $strFieldValue, $xml_parser, $patientResult;
var $strLastPatientTag, $strLastPatientInsurancePlanTag ;
function patientInfo($argLink)
{
$this->link = $argLink;
$this->xml_parser = xml_parser_create();
xml_set_object($this->xml_parser, $this);
xml_set_element_handler($this->xml_parser, "startElement", "endElement");
xml_set_character_data_handler($this->xml_parser, "characterData");
}
function startElement($parser, $name, $attrs)
{
if ($this->insidePatient)
{
$this->tag = $name;
}
elseif ($name == "PATIENT")
{
$this->insidePatient = true;
$this->strTableName = "tbl_patient";
}
if ($this->insidePatientInsurancePlan)
{
$this->tag = $name;
}
elseif ($name == "PATIENT_INSURANCE_PLAN")
{
$this->insidePatientInsurancePlan = true;
$this->strTableName = "tbl_patient_insurance_plan";
}
}
function endElement($parser, $name)
{
if ($name == "PATIENT")
{
$this->patientResult = $this->savePatientInfo();
$this->strFieldName="";
$this->strFieldValue="";
$this->strTableName="";
$this->insidePatient = false;
}
if ($name == "PATIENT_INSURANCE_PLAN")
{
$this->patientInsurancePlanResult = $this->savePatientInfo();
$this->strFieldName="";
$this->strFieldValue="";
$this->strTableName="";
$this->insidePatientInsurancePlan = false;
}
}
function savePatientInfo()
{
$query = "INSERT INTO ".$this->strTableName."(".$this->strFieldName.") ".
"VALUES (".$this->strFieldValue.")";
echo "<br>";
echo $query;
mysql_query($query) or die('Error, query failed');
}
function characterData($parser, $data)
{
if ($this->insidePatient && $this->strLastPatientTag != $this->tag )
{
switch ($this->tag)
{
case "INT_PATIENT_ID":
$this->strFieldName.= "int_patient_id,";
$this->strFieldValue.= $this->link->nextId('patientIdSequence').",";
break;
case "INT_LOGIN_ID":
$this->strFieldName.= "int_login_id,";
$this->strFieldValue.= $this->link->nextId('loginIdSequence').",";
break;
case "STR_PATIENT_SALUTATION":
$this->strFieldName.= "str_patient_salutation";
$this->strFieldValue.= "\"".$data."\"";
break;
}
echo "<BR>";
}
$this->strLastPatientTag = $this->tag;
if ($this->insidePatientInsurancePlan && $this->strLastPatientInsurancePlanTag != $this->tag )
{
switch ($this->tag)
{
case "INT_PATIENT_INSURANCE_PLAN_ID":
$this->strFieldName.= "int_patient_insurance_plan_id,";
$this->strFieldValue.= $this->link->nextId('patientInsurancePlanIdSequence').",";
break;
case "INT_PATIENT_LOGIN_ID":
$this->strFieldName.= "int_patient_login_id,";
$this->strFieldValue.= $this->link->nextId('patientLoginIdSequence').",";
break;
case "INT_INSURANCE_PLAN_ID":
$this->strFieldName.= "int_insurance_plan_id";
$this->strFieldValue.= $this->link->nextId('insurancePlanIdSequence')."";
break;
}
echo "<BR>";
}
$this->strLastPatientInsurancePlanTag = $this->tag;
}
function readPatientInfo($argStrNewFileName)
{
$fp = fopen($argStrNewFileName,"r") or die("Error reading RSS data.");
while ($data = fread($fp, filesize($argStrNewFileName)))
{
xml_parse($this->xml_parser, $data, feof($fp));
fclose($fp);
xml_parser_free($this->xml_parser);
$this->link->disconnect() or die("Can not close the database.");
}
}
/*function dbClose($link)
{
$link->disconnect() or die("Can not close the database.");
}*/
}
$dbName = "testdb";
$dbHost = "localhost";
$dbUser = "test";
$dbPassword = "test";
$dsn = "mysql://$dbUser:$dbPassword@$dbHost/$dbName";
$link = DB::connect($dsn);
if (DB::isError($link))
{
die ($link->getMessage());
}
$patientInfoResult = new patientInfo($link);
$patientInfoResult->readPatientInfo($strNewFileName);
?>