简体   繁体   English

使用PHP解析XML中具有空值的变量

[英]Using PHP to parse variables with a null value in xml

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. 我试图解析一个xml文件,该文件包含具有相同属性的每个varValue(如下所示)的多个元素。 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. 在下面的示例中,电子邮件和send_transcript均被排除在第二次会话之外。 My end goal is to import the data into oracle which works fine except for the cases I mentioned above. 我的最终目标是将数据导入到oracle中,除了上面提到的情况外,它都能正常工作。

Below is an example of the xml file which I'm working with. 以下是我正在使用的xml文件的示例。

<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">address1@myexample.com</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,address1@myexample.com,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? 如果我在代码的开头创建了变量$ email ='',那么它似乎忽略了该错误,但是它重复了以前的值而不是null?

Just initialize the variables before the second level foreach statements: 只需在第二级foreach语句之前初始化变量:

foreach($xml_object->Session as $session) {
    $email = '';
    $identifier = '';
    $sendTranscript = '';
    $sessionid = $session['realTimeID'];
    ...

Everything should work fine then! 一切都应该正常工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM