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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:48:34+00:00 2026-06-18T06:48:34+00:00

I am using an XML parser to store the xml feed datas into mysql

  • 0

I am using an XML parser to store the xml feed datas into mysql table and if I get an XML file result as an array, the result is shown below. How to store the each values into a variable.

Not this type of array only, If we use an different XML file or URL it’s result may be no of multidimensional arrays.

How to store the array values in php variables

   <?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
    $arrData = array();

    // if input is object, convert into array
    if (is_object($arrObjData)) {
        $arrObjData = get_object_vars($arrObjData);
    }

    if (is_array($arrObjData)) {
        foreach ($arrObjData as $index => $value) {
            if (is_object($value) || is_array($value)) {
                $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
            }
            if (in_array($index, $arrSkipIndices)) {
                continue;
            }
            $arrData[$index] = $value;
        }
    }
    return $arrData;
}


$xmlUrl = "testxmel.xml"; // XML feed file/URL
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);

Testxmel.xml

<?xml version="1.0" encoding="utf-8"?>
<everyone>
  <guest>
    <name>Joseph Needham</name>
    <age>53</age>
  </guest>
  <guest>
    <name>Lu Gwei-djen</name>
    <age>31</age>
  </guest>
</everyone>

Anybody can help me to solve this XML parser result problem. Thanks in advance

  • 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-18T06:48:35+00:00Added an answer on June 18, 2026 at 6:48 am

    Self-contained example using pdo

    <?php
    $pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    setup($pdo);
    
    $stmt = $pdo->prepare('INSERT INTO soFoo (name, age) VALUES (:name, :age)');
    $stmt->bindParam('name', $name);
    $stmt->bindParam('age', $age);
    
    
    $data = data();
    // number of elements in $data['guest']
    echo count($data['guest']), " new entries<br />\n";
    
    // add data to mysql database
    // iterate over elements in  $data['guest']
    foreach( $data['guest'] as $row ) {
        // assign "values" to the previously bound parameters
        $name = $row['name'];
        $age = $row['age'];
        // execute preapred statement
        $stmt->execute();
    }
    $stmt = null;
    
    // retrieve data: only count
    // if you only want count the records do not transfer the data from the mysql server to your php process - use Count(*) instead
    $row = $pdo->query('SELECT Count(*) from soFoo')->fetch();
    echo $row[0], " entries in database<br />\n";
    
    // retrieve data: actual data
    $stmt = $pdo->query('SELECT name, age from soFoo', PDO::FETCH_ASSOC);
    foreach( $stmt as $row ) {
        echo $row['name'], ' ', $row['age'], "<br />\n";
    }
    // stmt->rowCount() after a SELECT statement doesn't work with all pdo drivers, but for PDO_MySQL it does
    echo $stmt->rowCount(), ' records fetched';
    
    // boilerplate: create temporary database structure ...
    function setup($pdo) {
        $pdo->exec('
            CREATE TEMPORARY TABLE soFoo (
                id int auto_increment,
                name varchar(32),
                age int,
                primary key(id)
            )
        ');
    }
    
    // boilerplate: a function returning example data to work on...
    function data() {
        return array (
            'guest' => array (
                array (
                    'name' => 'Joseph Needham',
                    'age' => 53
                ),
                array (
                    'name' => 'Lu Gwei-djen',
                    'age' => 31
                )
            )
        );
    }
    

    edit: given your xml you could do something like

    <?php
    $pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    setup($pdo);
    
    $stmt = $pdo->prepare('INSERT INTO soFoo (name, age) VALUES (:name, :age)');
    $stmt->bindParam('name', $name);
    $stmt->bindParam('age', $age);
    
    // insert data
    $data = data();
    echo count($data->guest), " new entries<br />\n";
    foreach( $data->guest as $guest ) {
        $name = (string)$guest->name;
        $age = (string)$guest->age;
        $stmt->execute();
    }
    $stmt = null;
    
    // retrieve data: only count
    $row = $pdo->query('SELECT Count(*) from soFoo')->fetch();
    echo $row[0], " entries in database<br />\n";
    
    // retrieve data: actual data
    $stmt = $pdo->query('SELECT name, age from soFoo', PDO::FETCH_ASSOC);
    foreach( $stmt as $row ) {
        echo $row['name'], ' ', $row['age'], "<br />\n";
    }
    echo $stmt->rowCount(), ' records fetched';
    
    
    function setup($pdo) {
        $pdo->exec('
            CREATE TEMPORARY TABLE soFoo (
                id int auto_increment,
                name varchar(32),
                age int,
                primary key(id)
            )
        ');
    }
    
    function data() {
        // return simplexml_load_file('...');
        return new SimpleXMLElement( <<< eox
    <?xml version="1.0" encoding="utf-8"?>
    <everyone>
      <guest>
        <name>Joseph Needham</name>
        <age>53</age>
      </guest>
      <guest>
        <name>Lu Gwei-djen</name>
        <age>31</age>
      </guest>
    </everyone>
    eox
        );
    }
    

    output is the same.
    But keep in mind that simplxml_load_file() keeps the whole DOM in memory. If your data source gets large that might become a problem and you better switch to XMLReadery,

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

Sidebar

Related Questions

When using expat xml parser in python, how can I get it store the
I am using XML Parser to parse a xml file and store as Global
I need to get data from an XML file and store it into a
NOT USING A XML PARSER I want to get the contents of the XML
I want to parse the xml file with dynamic content using DOM parser in
I have student.xml file and am parsing this file using SAX Parser and now
I have a problem with using the SAX parser to parse a XML file.
I'm using simpleXML to parse this xml file . It's a feed I'm using
I am using an XML file to store data and parameters which is to
I am using ordinary XML parser and store the values of XML in a

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.