I have asked a few questions already which were quickly answered and have helped me get to this point however i've run into a little problem. Rather than asking another question in those I figured it was best to create a new question. I am attempting to parse an xml file which contains multiple elements for each varValue (shown below) with the same attributes. My script works fine when those values exist however if they are excluded I receive a warning. In the example below email and send_transcript were both excluded from the second session. My end goal is to import the data into oracle which works fine except for the cases I mentioned above.
Below is an example of the xml file which I'm working with.
<Report account="7869" start_time="2012-02-23T00:00:00+00:00" end_time="2012-02-23T15:27:59+00:00" user="twilson" more_sessions="false">
<Session id="ID742247692" realTimeID="4306650378">
<VarValues>
<varValue id="ID2051978" source="PreChat" sourceName="null" time="2012-02-23T00:07:07+00:00" name="identifier">Andy</varValue>
<varValue id="ID2051979" source="Internal" sourceName="null" time="2012-02-23T01:09:42+00:00" name="DisconnectedBy">VisitorClosedWindow</varValue>
<varValue id="ID2055925" source="PostChat" sourceName="null" time="2012-02-23T01:09:53+00:00" name="send_transcript">yes</varValue>
<varValue id="ID2055926" source="Operator" sourceName="null" time="2012-02-23T01:13:17+00:00" name="email">[email protected]</varValue>
<varValue id="ID2073144" source="PreChat" sourceName="null" time="2012-02-23T00:07:07+00:00" name="survey0373014">a group, team or business</varValue>
<varValue id="ID2074007" source="Operator" sourceName="null" time="2012-02-23T01:13:17+00:00" name="survey99630314">Pricing</varValue>
<varValue id="ID2075240" source="Operator" sourceName="null" time="2012-02-23T01:13:17+00:00" name="survey99630317">No</varValue>
<varValue id="ID2075243" source="Operator" sourceName="null" time="2012-02-23T01:13:17+00:00" name="survey99630320">Dont Know</varValue>
<varValue id="ID2083900" source="PostChat" sourceName="null" time="2012-02-23T01:09:53+00:00" name="survey99630223">none of the above</varValue>
<varValue id="ID2119346" source="Internal" sourceName="null" time="2012-02-23T00:06:20+00:00" name="LP_Visitor_Category">0</varValue>
<varValue id="ID2329945" source="PreChat" sourceName="null" time="2012-02-23T00:07:07+00:00" name="survey23360124">55379</varValue>
</VarValues>
</Session>
<Session id="ID742247695" realTimeID="4306650379">
<VarValues>
<varValue id="ID2051978" source="PreChat" sourceName="null" time="2012-02-23T00:04:37+00:00" name="identifier">Aram</varValue>
<varValue id="ID2051979" source="Internal" sourceName="null" time="2012-02-23T00:26:39+00:00" name="DisconnectedBy">RepStoppedChat</varValue>
<varValue id="ID2073144" source="PreChat" sourceName="null" time="2012-02-23T00:04:37+00:00" name="survey0373014">a group, team or business</varValue>
<varValue id="ID2074007" source="Operator" sourceName="null" time="2012-02-23T00:46:39+00:00" name="survey99630314">Turn Time</varValue>
<varValue id="ID2075240" source="Operator" sourceName="null" time="2012-02-23T00:46:39+00:00" name="survey99630317">No</varValue>
<varValue id="ID2075243" source="Operator" sourceName="null" time="2012-02-23T00:46:39+00:00" name="survey99630320">Likely</varValue>
<varValue id="ID2119346" source="Internal" sourceName="null" time="2012-02-23T00:04:23+00:00" name="LP_Visitor_Category">0</varValue>
<varValue id="ID2329945" source="PreChat" sourceName="null" time="2012-02-23T00:04:37+00:00" name="survey23360124">07452</varValue>
</VarValues>
</Session>
</Report>
Here is the code which I'm using to parse the file.
$xml_object = simplexml_load_file('C:/PHP/sessions.xml');
foreach($xml_object->Session as $session) {
$sessionid = $session['realTimeID'];
foreach($session->VarValues->varValue as $varValue) {
if($varValue['name'] == 'email') {
$email = (string) $varValue;
}
}
foreach($session->VarValues->varValue as $varValue2) {
if($varValue2['name'] == 'identifier') {
$identifier= (string) $varValue2;
}
}
foreach($session->VarValues->varValue as $varValue5) {
if($varValue5['name'] == 'send_transcript') {
$sendTranscript= (string) $varValue5;
}
}
$s = "$sessionid,'$email',$sendTransript'";
echo $s;
When I execute the script I am receiving a warning which states Undefined variable: email. As I mentioned above I'm receiving this message because the email field in our form is optional and often excluded.
My desired output would be as follows.
Sessionid|email|send_transcript
4306650378,[email protected],yes
4306650379,'',''
Anyone know how I can account for the null values? If I create a variable $email = '' at the beginning of my code then it seems to ignore the error however it repeats the previous value rather than null?