Jump to content

creating variables from a parsed xml doc


burge124

Recommended Posts

hi,

 

im parsing from an xml file and just need to know how to call those elements taken from the xml file into variables so that i can play around with them later?

 

//Initialize the XML parser

$parser=xml_parser_create();

 

//Function to use at the start of an element

function start($parser,$element_name,$element_attrs)

  {

  switch($element_name)

    {

    case "chapter":

    break;

    case "questiontitle":

    break;

    case "answera":

    break;

case "answerb":

    break;

case "answerc":

    break;

case "correctanswer":

    break;

    }

  }

 

from that information i want to create....

$chapter...

$qtitle....

$ansa....

$ansb...

and so on

 

thanks

Link to comment
Share on other sites

i use this XML class parser.

Makes stuff very easy

 

Ex of use:

function xml_to_db1($name,$attribs,$content) {
if ($content!=""){
$content = explode("<$name>", $content);
$content = explode("</$name>", $content[1]);
$content[0]=addslashes($content[0]);
}

if ($name=="Name"){
$enter = mysql_query("INSERT INTO coach (id, name) VALUE ('', '$content[0]')") or die (mysql_error());
}else{
if ($content!=""){
$coach_select = mysql_query("SELECT * FROM coach order by id desc limit 0,1") or die (mysql_error());
$coach = mysql_fetch_array($coach_select); 
$enter = mysql_query("UPDATE coach SET $name='$content[0]' WHERE id='$coach[id]'") or die (mysql_error());
}
}
}

$parser1 = new Path_parser();
$parser1->set_handler("/STHS-Coaches/Coach/Name","xml_to_db1");
$parser1->set_handler("/STHS-Coaches/Coach/Team","xml_to_db1");
$parser1->set_handler("/STHS-Coaches/Coach/Country","xml_to_db1");
$parser1->set_handler("/STHS-Coaches/Coach/Age","xml_to_db1");
$parser1->set_handler("/STHS-Coaches/Coach/PH","xml_to_db1");
$parser1->set_handler("/STHS-Coaches/Coach/DF","xml_to_db1");
$parser1->set_handler("/STHS-Coaches/Coach/OF","xml_to_db1");
$parser1->set_handler("/STHS-Coaches/Coach/PD","xml_to_db1");
$parser1->set_handler("/STHS-Coaches/Coach/EX","xml_to_db1");
$parser1->set_handler("/STHS-Coaches/Coach/LD","xml_to_db1");
$parser1->set_handler("/STHS-Coaches/Coach/PO","xml_to_db1");
$parser1->set_handler("/STHS-Coaches/Coach/Contrac","xml_to_db1");
$parser1->set_handler("/STHS-Coaches/Coach/Salary","xml_to_db1");
if(!$parser1->parse_file("include/xml/LNHSP3-Coaches.xml")) {
  print("Error:".$parser1->get_error()."<br/>\n"); 
}else{
echo "LNHSP3-Coaches.xml has been parsed!<br>";	
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.