<html>
<title>XML Test</title>
<body>
<?php
$MyParser = xml_parser_create();
//Function to use at the start of an element
function OpenTag($MyParser,$element_name,$element_attrs) {
switch($element_name) {
case "EMPLOYEE":
print "-- employee -- <br>";
break;
case "EMP_ID":
print "Emp ID : ";
break;
case "EMP_NAME":
print "First Name : ";
break;
case "EMP_LAST":
print "Last Name : ";
break;
case "USERNAME":
print "Username : ";
break;
case "PASSWORD":
print "Password : ";
break;
}
}
//Function to use at the end of an element
function EndTag($MyParser,$element_name){
print "<br>";
}
//Function to use when finding character data
function ReadData($MyParser,$data){
print $data;
}
//Specify element handler
xml_set_element_handler($MyParser,"OpenTag","EndTag");
//To OpenTag and EndTag function
//Specify data handler
xml_set_character_data_handler($MyParser,"ReadData");
// To ReadData function
//Open XML file
$fp=fopen("myxml.xml","r");
//Read data
while ($data=fread($fp,4096)) {
xml_parse($MyParser,$data,feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($MyParser)),
xml_get_current_line_number($MyParser)));
}
//Free the XML parser
xml_parser_free($MyParser);
?>
<?
require("C:\AppServ\www\XML\connect.php");
$counter = 0;
function myRecordHandler($MyParser)
{
global $counter;
global $STRsql;
$counter++;
$STRsql = "INSERT INTO employee (Emp_ID,Emp_Name,Emp_Last,Username,Password) VALUES (
'".$MyParser["Emp_ID"]."',
'".$MyParser["Emp_NAME"]."',
'".$MyParser["Emp_LAST"]."',
'".$MyParser["USERNAME"]."',
'".$MyParser["PASSWORD"]."'
)";
$sql = mysql_query($STRsql);
}
if($sql)
{
echo "Ok" ;
}
else
{
echo "error";
}
?>
</body>
</html>
<html>
<title>XML Test</title>
<body>
<?php
$MyParser = xml_parser_create();
//Function to use at the start of an element
function OpenTag($MyParser,$element_name) {
switch($element_name) {
case "EMPLOYEE":
print "-- employee -- <br>";
break;
case "EMP_ID":
print "Emp ID : ";
break;
case "EMP_NAME":
print "First Name : ";
break;
case "EMP_LAST":
print "Last Name : ";
break;
case "USERNAME":
print "Username : ";
break;
case "PASSWORD":
print "Password : ";
break;
}
}
//Function to use at the end of an element
function EndTag($MyParser,$element_name){
print "<br>";
}
//Function to use when finding character data
function ReadData($MyParser,$data){
print $data;
}
//Specify element handler
xml_set_element_handler($MyParser,"OpenTag","EndTag");
//To OpenTag and EndTag function
//Specify data handler
xml_set_character_data_handler($MyParser,"ReadData");
// To ReadData function
//Open XML file
$fp=fopen("myxml.xml","r");
//Read data
while ($data=fread($fp,4096)) {
xml_parse($MyParser,$data,feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($MyParser)),
xml_get_current_line_number($MyParser)));
}
//Free the XML parser
xml_parser_free($MyParser);
?>
<?
require("../XML/connect.php");
$filename="myxml.xml";
if(filesize($filename)>0)
{
$oDOM = new DOMDocument();
$oDOM->loadXML(file_get_contents($filename));
foreach ($oDOM->getElementsByTagName('emp') as $rest)
{
$sSQL = sprintf("INSERT INTO employee(Emp_ID, Emp_Name, Emp_Last,Username,Password) VALUES ('%s', '%s', '%s', '%s', '%s')",
mysql_real_escape_string($rest->getElementsByTagName('Emp_ID')->item(0)->nodeValue),
mysql_real_escape_string($rest->getElementsByTagName('Emp_Name')->item(0)->nodeValue),
mysql_real_escape_string($rest->getElementsByTagName('Emp_Last')->item(0)->nodeValue),
mysql_real_escape_string($rest->getElementsByTagName('Username')->item(0)->nodeValue),
mysql_real_escape_string($rest->getElementsByTagName('Password')->item(0)->nodeValue)
);
$rResult = mysql_query($sSQL);
if(mysql_errno() > 0)
{
printf(
'<h4 style="color: red;">Query Error:</h4>
<p>(%s) - %s</p>
<p>Query: %s</p>
<hr />',
mysql_errno(),
mysql_error(),
$sSQL
);
}
else
{
echo "OK";
}
}
}
?>
</body>
</html>