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

时间:2012-05-31 15:57:53

标签: php simplexml

我已经问了几个问题,这些问题很快得到了解答,并帮助我达到了这一点,但是我遇到了一个小问题。我没有在那些问题中提出另一个问题,而是最好创建一个新问题。我试图解析一个xml文件,其中包含具有相同属性的每个varValue(如下所示)的多个元素。当这些值存在时,我的脚本工作正常,但如果它们被排除,我会收到警告。在下面的示例中,email和send_transcript都被排除在第二个会话之外。我的最终目标是将数据导入oracle,除了我上面提到的情况之外,它工作正常。

下面是我正在使用的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>

这是我用来解析文件的代码。

   $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;

当我执行脚本时,我收到一条警告,指出未定义的变量:email。正如我上面提到的,我收到此消息是因为我们表单中的电子邮件字段是可选的,通常被排除在外。

我想要的输出如下。

Sessionid|email|send_transcript
4306650378,address1@myexample.com,yes
4306650379,'',''

任何人都知道如何解释空值?如果我在代码的开头创建一个变量$ email ='',那么它似乎忽略了错误,但它重复了之前的值而不是null?

1 个答案:

答案 0 :(得分:1)

只需在第二级foreach语句之前初始化变量:

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

一切都应该正常工作!