Sign in to follow this  

[web] parse XML into mysql

This topic is 4380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, i want to make a script with which i open an xml file and write its data into mysql. i try the following: use strict; use DBI; use XML::Parser; # create hash to hold values for expected column names my %row = ("name" => undef, "category" => undef); # connect to database and create parser object my $dbh = DBI->connect ("DBI:mysql:test", "testuser", "testpass", { RaiseError => 1, PrintError => 0}); my $parser = new XML::Parser ( Handlers => { Start => \&handle_start, End => \&handle_end, Char => \&handle_text } ); # parse file and disconnect $parser->parsefile ("animal.xml"); $dbh->disconnect (); to read an xml like: <?xml version="1.0"?> <dataset> <row> <name>snake</name> <category>reptile</category> </row> <row> <name>frog</name> <category>amphibian</category> </row> <row> <name>tuna</name> <category>fish</category> </row> <row> <name>racoon</name> <category>mammal</category> </row> </dataset> but i get Parse error: parse error, expecting `T_CONSTANT_ENCAPSED_STRING' or `'('' in c:\phpdev\www\untitled-1.php on line why?? PS:How can write code in a frame when i post??cause its not nice to write it like the way i wrote it.

Share this post


Link to post
Share on other sites
Your code looks like Perl, but you are trying to execute it as if it is PHP, that is not going to work.
Use [[b]source]code here[/source] to put the code in a box.

Share this post


Link to post
Share on other sites
Hi again.

i try to parse an xml file but i have a problem.

the xml file is like this:
<?xml version='1.0' encoding="ISO-8859-7"?>

<!-- File name eidisi.xml -->

<EIDISI>
<DIMOSIOGRAFOS>babis karipidis</DIMOSIOGRAFOS>
<TITLOS>titlos</TITLOS>
<HMEROMINIA>
<HMERA>4 </HMERA>
<MINAS>12 </MINAS>
<ETOS>2006 </ETOS>
</HMEROMINIA>
<KATIGORIA>a8litika</KATIGORIA>
<KEIMENO><![CDATA[keimeno]]></KEIMENO>
</EIDISI>



i can normal get all the tags contents expept from the last 2 KATIGORIA and KEIMENO..I dont understand why.. pls help..

here is my code:

<?php
//---------------------------PARSING TOY XML------------------------------------//
// the xml file we want to parse
$xmlSource="eidisi.xml";


//First we define a number of variables to store the data from each element

$DIMOSIOGRAFOS="";
$TITLOS="";
$KATIGORIA="";
$KEIMENO="";
$HMERA="";
$MINAS="";
$ETOS="";


$currentElement=""; //holds the name of the current element

$shows=array(); //array to hold all the movie data

/* The start Element Handler
* This is where we store the element name, currently being parsed, in $currentElement.
* This is also where we get the attribute, if any.
*/

function startElement($parser,$name,$attr){

$GLOBALS['currentElement']=$name;


}//end startElement()


/*
* The end Element Handler
*/


function endElement($parser,$name){
$elements=array('DIMOSIOGRAFOS','TITLOS','KATIGORIA','KEIMENO','HMERA','MINAS','ETOS');


/*If the element being parsed is a show it means that the

*parser has completed parsing show. We can then store

* the data in our array $shows[ ]

*/


if(strcmp($name,"HMEROMINIA")==0){
foreach($elements as $element){
$temp[$element]=$GLOBALS[$element];
}
$GLOBALS['shows'][]=$temp;



/*After storing the data we reset our global show-variables to
hold a new show*/


$GLOBALS['HMERA']="";

$GLOBALS['MINAS']="";

$GLOBALS['ETOS']="";



}

/*After parsing a movie we reset the rest of the globals.*/

if(strcmp($name,"EIDISI")==0){

$GLOBALS['DIMOSIOGRAFOS']="";

$GLOBALS['TITLOS']="";

$GLOBALS['KATIGORIA']="";

$GLOBALS['KEIMENO']="";



}

}//end endElement()


/* The character data Handler
* Depending on what the currentElement is,
* the handler assigns the value to the appropriate variable
*/


function characterData($parser, $data) {
$elements = array (
'DIMOSIOGRAFOS', 'TITLOS', 'KATIGORIA',
'KEIMENO', 'HMERA', 'MINAS', 'ETOS'
);

foreach ($elements as $element) {
if ($GLOBALS["currentElement"] == $element) {
$GLOBALS[$element] .= $data;
}
}
}

/* This is where the actual parsing is going on.
* parseFile() parses the xml document and return an array
* with the data we asked for.
*/


function parseFile(){
global $xmlSource,$shows;

/*Creating the xml parser*/
$xml_parser=xml_parser_create();

/*Register the handlers*/
xml_set_element_handler($xml_parser,"startElement","endElement");
xml_set_character_data_handler($xml_parser,"characterData");

/*Disables case-folding. Needed for this example*/
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,false);

/*Open the xml file and feed it to the parser in 4k blocks*/
if(!($fp=fopen($xmlSource,"r"))){
die("Cannot open $xmlSource ");
}
while(($data=fread($fp,4096))){

if(!xml_parse($xml_parser,$data,feof($fp))){
die(sprintf("XML error at line %d column %d ",
xml_get_current_line_number($xml_parser),
xml_get_current_column_number($xml_parser)));
}
}

/*Finish ! we free the parser and returns the array*/
xml_parser_free($xml_parser);

return $shows;

}//end parseFile()



/***************************************************************************************************
Calling the parseFile() and getting the result out
***************************************************************************************************/


$result=parseFile();
foreach($result as $arr)
{
$DIMOSIOGRAFOS_VASI=$arr["DIMOSIOGRAFOS"];
echo "$DIMOSIOGRAFOS_VASI<br>";
$TITLOS_VASI=$arr["TITLOS"];
echo "$TITLOS_VASI<br>";
$KATIGORIA_VASI=$arr["KATIGORIA"];
echo "$KATIGORIA_VASI<br>";
$KEIMENO_VASI=$arr["KEIMENO"];
echo "$KEIMENO_VASI<br>";
$ETOS_VASI=$arr["ETOS"];
echo "$ETOS_VASI<br>";

}



?>


thanks in advance.

Share this post


Link to post
Share on other sites

This topic is 4380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this