Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Steps needed to read a XML file and Insert that data into MySQL

Status
Not open for further replies.
May 13, 2005
56
US
Hi there,

I have been trying to find a way to do this that I can comprehend, it seems to be quite a bit over my limited web programming skill level.

Here is what I am working with.
-Fedora Core 3 server with PHP and MySQL installed
-A site that exports its data to XML for custom use
-A need to take this data and enter it into mySQL db for different uses

So, where I am tripping up is getting the data from the xml page to my DB :)

For example: this document has data I want to store in my DB..

I have read some about XML parsing but I am having a tough time grasping the entire process or how to implement it..

Any help at all would be greatly appriciated.

Thank you.
 
Do a google for "PHP XML". This link looks fairly straight-forward:
Code:
[URL unfurl="true"]http://www.scit.wlv.ac.uk/~jphb/sst/php/proglang/xml.html[/URL][/code

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
OK, what about this?

I want to take this file and parse it to display all data on a php page, the following code was copied from here ( and I changed it to work for the current XML doc. (I have to much or too little though??

Code:
<?php
$file = "[URL unfurl="true"]http://football.myfantasyleague.com/2004/export?TYPE=players";[/URL]
$usercount = 0;
$matchupcount = 0;
$userdata = array();
$state = '';

function startElement ($parser,$name,$attrib){
global $usercount;
global $matchupcount;
global $userdata;
global $state;

switch ($name) {
case $name=="PLAYER" : {
$userdata[$matchupcount]["id"] = $attrib["ID"];
$userdata[$matchupcount]["name"] = $attrib["NAME"];
$userdata[$matchupcount]["position"] = $attrib["POSITION"];
$userdata[$matchupcount]["team"] = $attrib["TEAM"];
}
break;
}
default : {$state=$name;break;}
}


function endElement ($parser,$name){
global $usercount;
global $matchupcount;
global $userdata;
global $state;
$state='';
if($name=="PLAYER") {$usercount++;}
}

function characterData ($parser, $data) {
global $usercount;
global $userdata;
global $state;
if (!$state) {return;}
}
$simpleparser = xml_parser_create('UTF-8');
xml_set_element_handler($simpleparser, "startElement", "endElement");
xml_set_character_data_handler($simpleparser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}

while (!feof($fp)) {
$data = fgets($fp);
if (!xml_parse($simpleparser, $data, feof($fp))) {
 die(xml_error_string(xml_get_error_code($simpleparser)));
}
}
for ($i=0;$i<$matchupcount; $i++) {
$player = $userdata[$i]["id"];
$name = $userdata[$i]["name"];
$position = $userdata[$i]["position"];
$team = $userdata[$i]["team"];
print "Player: $player Name: $name Position: $position Team: $team<br>";
}
xml_parser_free($simpleparser);
?>

Where am I going wrong here?

Thanks..

Mike
 
I was able to get this working..

Code:
<?php
$file = "[URL unfurl="true"]http://football.myfantasyleague.com/2004/export?TYPE=players";[/URL]
$playercount = 0;
$userdata = array();
$state = '';

function startElement ($parser,$name,$attrib){
global $playercount;
global $userdata;
global $state;

switch ($name) {
case $name=="PLAYER" : {
$userdata[$playercount]["id"] = $attrib["ID"];
$userdata[$playercount]["name"] = $attrib["NAME"];
$userdata[$playercount]["position"] = $attrib["POSITION"];
$userdata[$playercount]["team"] = $attrib["TEAM"];
break;
}
default : {$state=$name;break;}
}
}

function endElement ($parser,$name){
global $playercount;
global $userdata;
global $state;
$state='';
if($name=="PLAYER") {$playercount++;}
}

function characterData ($parser, $data) {
global $userdata;
global $state;
if (!$state) {return;}
}

$simpleparser = xml_parser_create('UTF-8');
xml_set_element_handler($simpleparser, "startElement", "endElement");
xml_set_character_data_handler($simpleparser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}

while (!feof($fp)) {
$data = fgets($fp);
if (!xml_parse($simpleparser, $data, feof($fp))) {
die(xml_error_string(xml_get_error_code($simpleparser)));
}
}
for ($i=0;$i<$playercount; $i++) {
$player = $userdata[$i]["id"];
$name = $userdata[$i]["name"];
$position = $userdata[$i]["position"];
$team = $userdata[$i]["team"];
print "Player: $player  Name: $name  Position: $position  Team: $team<br>";
}
xml_parser_free($simpleparser);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top