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 772713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:50:44+00:00 2026-05-14T18:50:44+00:00

I am trying to parse an xml file using XmlReader but although I am

  • 0

I am trying to parse an xml file using XmlReader but although I am getting a return from the xml file for the (commission) node for some reason I am getting an empty SimpleXMLElement Object returned as well. I don’t know if its something to do with while loop,switch or something I missed in the parse setup.

This is the xml file I am trying to read from, as you can see there is only 1 result returned:

<?xml version="1.0" encoding="UTF-8"?>
<cj-api>
    <commissions total-matched="1">
        <commission>
            <action-status>
                new
            </action-status>
            <action-type>
                lead
            </action-type>
            <aid>
                10730981
            </aid>
            <commission-id>
                1021015513
            </commission-id>
            <country>
            </country>
            <event-date>
                2010-05-08T08:08:55-0700
            </event-date>
            <locking-date>
                2010-06-10
            </locking-date>
            <order-id>
                345007
            </order-id>
            <original>
                true
            </original>
            <original-action-id>
                787692438
            </original-action-id>
            <posting-date>
                2010-05-08T10:01:22-0700
            </posting-date>
            <website-id>
                3201921
            </website-id>
            <cid>
                2815954
            </cid>
            <advertiser-name>
                SPS EurosportBET
            </advertiser-name>
            <commission-amount>
                0
            </commission-amount>
            <order-discount>
                0
            </order-discount>
            <sid>
                0
            </sid>
            <sale-amount>
                0
            </sale-amount>
        </commission>
    </commissions>
</cj-api>

This is my parser:

   <?php

    // read $response (xml feed)
    $file = "datafeed.xml";
    $xml = new XMLReader;

    $xml->open($file);

    // loop to read in data

    while ($xml->read()) {

            switch ($xml->name) {

            // find the parent node for each commission payment
                case 'commission':
            // initalise xml parser
                    $dom = new DomDocument(); 
                    $dom_node = $xml ->expand();
                    $element = $dom->appendChild($dom_node); 
                    $dom_string = $dom->saveXML($element); 
                    $commission = new SimpleXMLElement($dom_string);

                    // read in data

                    $action_status = $commission->{'action-status'};
                    $action_type = $commission->{'action-type'};
                    $aid = $commission->{'aid'};
                    $commission_id = $commission->{'commission-id'};
                    $country = $commission->{'country'};
                    $event_date = $commission->{'event-date'};
                    $locking_date = $commission->{'locking-date'};
                    $order_id = $commission->{'order-id'};
                    $original = $commission->{'original'};
                    $original_action_id = $commission->{'original_action-id'};
                    $posting_date = $commission->{'posting-date'};
                    $website_id = $commission->{'website-id'};
                    $cid = $commission->{'cid'};
                    $advertiser_name = $commission->{'advertiser-name'};
                    $commission_amount = $commission->{'commission-amount'};
                    $order_discount = $commission->{'order-discount'};
                    $sid = $commission->{'sid'};
                    $sale_amount = $commission->{'sale-amount'};


                print_r($aid);
                    break;

            }

    }
    ?>

The result is :

SimpleXMLElement Object ( [0] => 10730981 ) SimpleXMLElement Object ( )

Why is it returning the second object: SimpleXMLElement Object ( ) and what do I need to do correct it? Thanks.

  • 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-05-14T18:50:45+00:00Added an answer on May 14, 2026 at 6:50 pm

    You are hitting the loop twice for opening and closing tags. You need to check the nodeType, like,

      if ($xml->name == 'commission' && $xml->nodeType == XMLReader::ELEMENT) {
         // Process the node
      }
    

    I don’t know what you are trying to accomplish by using a mixture of XMLReader, DOM and SimpleXML. Why don’t you just use SimpleXML, like

       $xml = simplexml_laod_file($file);
       $commission = $xml->commissions[0]->commission[0];
       $aid = $commission->{'aid'};
       print_r($aid);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 449k
  • Answers 449k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want to save an object's ID permanently you… May 15, 2026 at 8:08 pm
  • Editorial Team
    Editorial Team added an answer Found the solution. Just change to: public class PrintUnicode {… May 15, 2026 at 8:08 pm
  • Editorial Team
    Editorial Team added an answer The Uptime article on Wikipedia has an interesting lead: Using… May 15, 2026 at 8:08 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.