3
votes

I need to parse an xml file in order to display an attribute based on a key value. For example, in the file below I would like to extract realTimeIid and email for each session however I'm having a difficult time getting my code to work due to the fact that there are multiple elements for each varValue with the same attributes.

<?xml version="1.0" encoding="UTF-8" ?> 
<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="ID2055926" source="Operator" sourceName="null" time="2012-02-23T00:46:39+00:00" name="email">[email protected]</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 my code using DOMDocument. My desired results should return two rows as follows.

Sessionid|email
4306650378 [email protected]
4306650379 [email protected]

I only want the email address and not all of the other elements and attributes under varValue->getAttribute('name'). The problem with this is that it returns all of the attributes and I don't know how to look for just email and get the subsequent value.

  $doc = new DOMDocument();
  $doc->load( 'C:/Dev/report.xml' );

  $sessions = $doc->getElementsByTagName( "Session" );
  foreach( $sessions as $session )
  {

  $sessionid = $session->getAttribute( 'realTimeID' );

  $values = $session->getElementsByTagName( "VarValues" );
  foreach( $values as $value)

  {   

  $varValues = $value->getElementsByTagName( "varValue" );
  foreach( $varValues as $other )

  {

  $email = $other->getAttribute('name');

  echo "$sessionid- $email\n"; 
  }}}
2
Just for curiosity, what is the source of this XML ?Shashank Kadne

2 Answers

2
votes

I've always found SimpleXML to be really easy to work with:

<?php

$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8" ?> 
<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="ID2055926" source="Operator" sourceName="null" time="2012-02-23T00:46:39+00:00" name="email">[email protected]</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;

$xml_object = simplexml_load_string($xml_string);

foreach($xml_object->Session as $session) {
    $sessionid = $session['realTimeID'];
    foreach($session->VarValues->varValue as $varValue) {
        if($varValue['name'] == 'email') {
            $email = (string) $varValue;
            echo $sessionid.'- '.$email."\n";
            break;
        }
    }
}

?>
0
votes

Using xpath can be a easier solutions. please see this link below for example

click here

Thanks.