Jump to content

[SOLVED] SESSION VARIABLE


AnotherQuestion

Recommended Posts

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>

 

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'];

 

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.