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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing XML How?

Status
Not open for further replies.

soodvarun

Programmer
Apr 21, 2001
40
CA
I have bunch of XML files and I want to develop a keyword based search engine which parses those XML files.

Do you have any idea which is the best way to do the job? I mean on Linux? Should I use PHP? JAVA? Python??

Please let me know. I am very new to everything in this domain.

Thanks Varun


 
I dont have a ton of experience with this but I can post some code on what I have found.

This takes a 5 digit league id used for myfantasyleague.com and parses the info I need with the built in PHP XML parser. I then create variables for the parsed data and eventually (not in this example, dump them into a mysql DB)

The XML file is located here
Code:
<?php

if (isset($_POST['league'])) {
  $league=$_POST['league'];

$file = "[URL unfurl="true"]http://football.myfantasyleague.com/2005/export?L=$league&TYPE=league";[/URL]
$matchupcount = 0;
//$positioncount = 0;
$userdata = array();
$state = '';

function startElement ($parser,$name,$attrib){
global $number;
global $matchupcount;
//global $positioncount;
global $userdata;
global $state;
$test =($number+1);

switch ($name) {
case $name=="LEAGUE" : {
$userdata[$matchupcount]["lid"] = $attrib["ID"];
$userdata[$matchupcount]["lname"] = $attrib["NAME"];
$userdata[$matchupcount]["lsize"] = $attrib["ROSTERSIZE"];
$userdata[$matchupcount]["last"] = $attrib["LASTREGULARSEASONWEEK"];
break;
}
//case $name=="STARTERS" : {
//$userdata[$matchupcount]["pos_count"] = $attrib["COUNT"];
//break;
//}
case $name=="POSITION" : {
$userdata[$matchupcount]["pos"][] = $attrib["NAME"];
break;
}
case $name=="FRANCHISES" : {
$userdata[$matchupcount]["count"] = $attrib["COUNT"];
break;
}
case $name=="FRANCHISE" : {
$userdata[$matchupcount]["fid"][] = $attrib["ID"];
$userdata[$matchupcount]["fname"][] = $attrib["NAME"];
break;
}


default : {$state=$name;break;}
}
}

function endElement ($parser,$name){
global $matchupcount;
//global $positioncount;
global $userdata;
global $state;
$state='';
//if($name=="POSITION") {$positioncount++;}
//}
if($name=="FRANCHISES") {$matchupcount++;}
}

function characterData ($parser, $data) {
global $matchupcount;
//global $positioncount;
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)));
}
}

$b=0;
for ($i=0;$i<$matchupcount;$i++) {
	$leagueid = $userdata[$i]["lid"];
	$lname = $userdata[$i]["lname"];
	$lsize = $userdata[$i]["lsize"];
	$last = $userdata[$i]["last"];
	$fran = $userdata[$i]["count"];
	//$pos_count = $userdata[$i]["pos_count"];
	print "League ID: $leagueid <br>";
	print "League Name: $lname <br>";
	print "Teams: $fran <br>";
	print "Roster Size: $lsize <br>";
	print "Last Week: $last <br>";
	//print "Starters: $pos_count <br>";
	print "<br>";
     for ($a=0;$a<$fran;$a++) {
         $rand=generateRandStr(6);
         $player[] = $userdata[$i]["fid"][$a];
		 $franchise[] = $userdata[$i]["fname"][$a];
         $pos[] = $userdata[$i]["pos"][$a];
		  echo "Team: $player[$b] $franchise[$b] <br>\n";
         $b++;
    }
}
echo "<br>";
//$count_pos=count($pos);
//print $count_pos;
//for ($i=0;$i<$positioncount;$i++) {
//	$p=$i+1;
//	print "Position $p : $pos[$i] <br>";
//}


xml_parser_free($simpleparser);
}else{
  print "error";
}
?>

Hope this helps,

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top