Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7984761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:22:30+00:00 2026-06-04T11:22:30+00:00

I need to parse an xml file in order to display an attribute based

  • 0

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">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="ID2055926" source="Operator" sourceName="null" time="2012-02-23T00:46:39+00:00" name="email">address2@myexample.com</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 address1@myexample.com
4306650379 address2@myexample.com

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"; 
  }}}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:22:32+00:00Added an answer on June 4, 2026 at 11:22 am

    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">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="ID2055926" source="Operator" sourceName="null" time="2012-02-23T00:46:39+00:00" name="email">address2@myexample.com</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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse content.xml file in XMind package. File looks like this <xmap-content
Hi I need to parse XML file using jquery. I created read and display
I have an XML file which I need to parse using PHP and send
I need to parse a value from an xml and place it to image's
Given the XML below, I need to parse it and output names of all
Deal all, Assume I have a XML file and I need to parse it
Sometimes I need to parse XML file - and only parse, and I don't
I have 2 questions: 1-I need to parse XML file and insert the data
I need to parse a xml file which is practically an image of a
We need to parse an XML file with XSLT into a CVS file. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.