AnotherQuestion Posted September 29, 2009 Share Posted September 29, 2009 I am trying to read an xml file and set the tags of the xml file to session variables. I would like to know how to set a session variable name eg $tagname $_SESSION['$tagname'] = "WHATEVER" ; Thanks Quote Link to comment https://forums.phpfreaks.com/topic/175978-solved-session-variable/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 29, 2009 Share Posted September 29, 2009 $_SESSION[$tagname] = "WHATEVER" ; Variables are not parsed when enclosed in single-quotes Quote Link to comment https://forums.phpfreaks.com/topic/175978-solved-session-variable/#findComment-927266 Share on other sites More sharing options...
AnotherQuestion Posted September 29, 2009 Author Share Posted September 29, 2009 Thanks for that I have tried the following code but not working as I mentioned earlier I am trying to read an xml file then set each of the tag names to a session variable and then set the data of the tags to the session name eg XML output <VRM>AMS1</VRM> would become: $_SESSION['VRM'] = "AMS1" etc. : (please see below for XML output $data = file_get_contents("https://vrm.mamsoft.co.uk/vrmlookup/vrmlookup.asmx/Find?Username=NOSPAM&Password=NOSPAM&Vrm=".$_GET['RegNo']); $FileName = "Vrmdata.xml"; $VrmFile = fopen($FileName, 'w')or die("can't open file"); fwrite($VrmFile, $data); fclose($VrmFile); //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) { } } //Function to use at the end of an element function stop($parser,$element_name) { } //Function to use when finding character data function char($parser,$data) { $_SESSION[$element_name] = $data; } //Specify element handler xml_set_element_handler($parser,"start","stop"); //Specify data handler xml_set_character_data_handler($parser,"char"); //Open XML file $fp=fopen("Vrmdata.xml","r"); //Read data while ($data=fread($fp,4096)) { xml_parse($parser,$data,feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser))); } //Free the XML parser xml_parser_free($parser); The output of the above file gives a result like: <?xml version="1.0" encoding="utf-8"?> <ReturnValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="Vehicle" xmlns="www.mamsoft.co.uk/VrmLookup"> <Vrm></Vrm> <Vin></Vin> <EngineNo></EngineNo> <EngineSize></EngineSize> <EngineModel></EngineModel> <Fuel></Fuel> <Make></Make> <Model></Model> <Colour></Colour> <Transmission>C</Transmission> <TransmissionCode></TransmissionCode> <BodyPlan></BodyPlan> <Gears></Gears> <YearOfManufacture></YearOfManufacture> <DateRegistered></DateRegistered> <Scrapped></Scrapped> <Exported></Exported> <Imported></Imported> <DtpMakeCode></DtpMakeCode> <DtpModelCode></DtpModelCode> </ReturnValue> Quote Link to comment https://forums.phpfreaks.com/topic/175978-solved-session-variable/#findComment-927282 Share on other sites More sharing options...
PFMaBiSmAd Posted September 29, 2009 Share Posted September 29, 2009 Inside of the function char(), the variable $element_name does not exist. Please develop and debug php code on a system with error_reporting set to E_ALL and display_errors set to ON so that php will help you. Quote Link to comment https://forums.phpfreaks.com/topic/175978-solved-session-variable/#findComment-927289 Share on other sites More sharing options...
AnotherQuestion Posted September 29, 2009 Author Share Posted September 29, 2009 worked it out $xmlDoc = new DOMDocument(); $xmlDoc->load("Vrmdata.xml"); $x = $xmlDoc->documentElement; foreach ($x->childNodes AS $item) { $_SESSION[$item->nodeName] = $item->nodeValue ; } $_SESSION['RegNo'] = $_SESSION['Vrm']; Quote Link to comment https://forums.phpfreaks.com/topic/175978-solved-session-variable/#findComment-927318 Share on other sites More sharing options...
PFMaBiSmAd Posted September 29, 2009 Share Posted September 29, 2009 I'm guessing the $_SESSION variable was from one of the other threads using the xml_parser_create(). The reason for it in that code was because the start() and char() callback functions are completely separate (unless you make that a class) and the easiest way of sharing information between the two functions was to use a $_SESSION variable, which is a global variable. For the code you just posted, you can use any array variable you choose to make. Quote Link to comment https://forums.phpfreaks.com/topic/175978-solved-session-variable/#findComment-927329 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.