我已经问了几个问题,这些问题很快得到了回答,并帮助我达到了这一点,但是我遇到了一个小问题。我认为最好创建一个新问题,而不是在这些问题中提出另一个问题。我正在尝试解析一个 xml 文件,该文件包含具有相同属性的每个 varValue(如下所示)的多个元素。当这些值存在时,我的脚本可以正常工作,但是如果它们被排除,我会收到警告。在下面的示例中,电子邮件和 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;
当我执行脚本时,我收到一条警告,指出未定义变量:电子邮件。正如我上面提到的,我收到此消息是因为我们表单中的电子邮件字段是可选的并且通常被排除在外。
我想要的输出如下。
Sessionid|email|send_transcript
4306650378,address1@myexample.com,yes
4306650379,'',''
有人知道我如何解释空值吗?如果我在代码的开头创建一个变量 $email = '' 那么它似乎忽略了错误但是它重复了以前的值而不是 null?