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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:43:37+00:00 2026-06-03T16:43:37+00:00

I have a pretty large file that I need to parse in php, so

  • 0

I have a pretty large file that I need to parse in php, so I’m using xmlReader. I’ve just started playing around with xml and am having some problems….

Here is how the xml is structured…

<?xml version="1.0" encoding="utf-8"?>
    <devices>
        <os name="os1">
            <device user_agent="uaValue1">
                <device_name>name1</device_name>
                <device_os>osValue1</device_os>
            </device>
            <device user_agent="uaValue2">
                <device_name>name2</device_name>
                <device_os>osValue2</device_os>
            </device>   
        </os>
        <os name="os2">
            <device user_agent="uaValue3">
                <device_name>name3</device_name>
                <device_os>osValue5</device_os>
            </device>
            <device user_agent="uaValue4">
                <device_name>name4</device_name>
                <device_os>osValue4</device_os>
            </device>   
        </os>
        <manufacturer name="mf1">
            <device user_agent="uaValue5">
                <device_name>name5</device_name>
                <device_os>osValue5</device_os>
            </device>
            <device user_agent="uaValue6">
                <device_name>name6</device_name>
                <device_os>osValue6</device_os>
            </device>       
        </manufacturer>
        <manufacturer name="mf2">
            <device user_agent="uaValue7">
                <device_name>name7</device_name>
                <device_os>osValue7</device_os>
            </device>
            <device user_agent="uaValue8">
                <device_name>name8</device_name>
                <device_os>osValue8</device_os>
            </device>       
        </manufacturer> 
    </devices>

I am trying to use the following function to read the xml (where $findby will pass os or manufacturer / $atr_value is the attribute name for the $findby value / $ua_str is the user agent).

function get_data($findby,$atr_value,$ua_str) {
    $data['device_name'] = '';
    $data['device_os'] = '';

    $xml = new XMLReader;
    $xml->open('devices.xml');

    while($xml->read()) {
        if ($xml->name == $findby) {
            if ($xml->getAttribute('name') == $atr_value) {
                while($xml->read()) {
                    if ($xml->name == "device") {
                        if ($xml->getAttribute('user_agent') == $ua_str) {
                            while($xml->read()) {                               
                                if ($xml->name == "device_name") {
                                    $data['device_name'] = $xml->value;
                                }
                                if ($xml->name == "device_os") {
                                    $data['device_os'] = $xml->value;
                                }
                            }
                        }
                    }                   
                }
            }
        }
    }
    $xml->close();
    return $data;
}

By echoing a few values here and there, it seems to pull data down to the user agent, but won’t get the device_name or device_os.

Obviously, this is not working. I was thinking that the xml can’t be structured that way which could be the problem and an easy fix. However on one of my previous versions, it pulled the data but it was from the wrong user agent. So I’m thinking it is something in how I am trying to pull the data with the xmlReader.

Does anyone have any thoughts on why this does not work? Thank you…

  • 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-03T16:43:38+00:00Added an answer on June 3, 2026 at 4:43 pm

    You’re not checking the tag type in the outer while loop. XMLReader sends you opening tags, closing tags, tag content, and you need to handle at least some of those events.

    In your outer loop, check $reader->nodeType == XMLReader::ELEMENT for an opening, XMLReader::END_ELEMENT for a closing, and XMLReader::TEXT for tag content. There are a lot of others too – see the PHP docs for this class (values 0-17).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this pretty large XML file (40MB) that I'll have to repeatedly
I have a relatively large csv/text data file (33mb) that I need to do
I have an application that is pretty memory hungry. It holds a large amount
I have an application that stores large amount of files (XML and binary) in
I have a pretty sizeable object graph that I have serialized to a file
I have a pretty large directory /var/www/ on my linux Ubuntu server. In that
I have a pretty large site and every page is built from several included
I'm looking at building a Rails application which will have some pretty large tables
I have a bunch of pretty large CSV (comma separated values) files and I
I have pretty big background of .net, and I've decided that i want to

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.