Jump to content
  • Advertisement
Sign in to follow this  

[web] parse XML into mysql

This topic is 4685 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 [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 -->

<ETOS>2006 </ETOS>

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:

//---------------------------PARSING TOY XML------------------------------------//
// the xml file we want to parse

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


$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){


}//end startElement()

* The end Element Handler

function endElement($parser,$name){

/*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[ ]


foreach($elements as $element){

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





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







}//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 (

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*/

/*Register the handlers*/

/*Disables case-folding. Needed for this example*/

/*Open the xml file and feed it to the parser in 4k blocks*/
die("Cannot open $xmlSource ");

die(sprintf("XML error at line %d column %d ",

/*Finish ! we free the parser and returns the array*/

return $shows;

}//end parseFile()

Calling the parseFile() and getting the result out

foreach($result as $arr)
echo "$TITLOS_VASI<br>";
echo "$KATIGORIA_VASI<br>";
echo "$KEIMENO_VASI<br>";
echo "$ETOS_VASI<br>";



thanks in advance.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!