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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:20:44+00:00 2026-06-15T11:20:44+00:00

I want to save data from a xml file to my database, and below

  • 0

I want to save data from a xml file to my database, and below you can see my php code where i’ve tried to create an associative array to put the values into for later use. However, the actual SimpleXMLElement object is stored, not the just the pure value.

I have two questions here:

  1. How can I change my code so that the actual values (as numbers or strings) is stored instead of the SimpleXMLElement object?
  2. Is this a good way of storing the values for later storing with SQLLite (which I will use), or what other options do I have that works better?

php:

$theProducers = simplexml_load_file('sources/producers.xml');

$i = 0;
foreach ($theProducers->producer as $producer) {
    $producers['id'][$i] = $producer->attributes();
    $producers['name'][$i] = $producer->name;
    $producers['address'][$i] = $producer->address;
    $producers['zipcode'][$i] = $producer->zipcode;
    $producers['town'][$i] = $producer->town;
    $producers['url'][$i] = $producer->url;
    $producers['imgurl'][$i] = $producer->imgurl;

    $i += 1;
}

print_r($producers);  // outcome below

producers.xml:

<?xml version="1.0"?>
<producers>
  <producer id="8">
    <name>Em&#xE5;mejeriet</name>
    <address>Grenv&#xE4;gen 1-3</address>
    <zipcode>577 39</zipcode>
    <town>Hultsfred</town>
    <url>http://www.emamejeriet.se</url>
    <imgurl>http://172.16.206.1/~thajo/1DV449/laboration01/producenter/images/ema.png</imgurl>
  </producer>
  <producer>
  ...

outcome from print_r($producers):

Array (
[id] => Array (
    [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 8 ) )
    [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 57 ) )
    [2] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 45 ) )
    [3] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 33 ) )
    [4] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 16 ) )
    [5] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 41 ) )
    [6] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 38 ) )
    [7] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 40 ) )
    [8] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 56 ) ) )
[name] => Array (
    [0] => SimpleXMLElement Object ( [0] => Emåmejeriet )
    [1] => SimpleXMLElement Object ( [0] => Ölands Örtagård AB )
    [2] => SimpleXMLElement Object ( [0] => Dövestads utegrisar & Gårdsbutik )
... and so on...
  • 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-15T11:20:45+00:00Added an answer on June 15, 2026 at 11:20 am

    change structure of the out put array:

    foreach ($theProducers->producer as $producer) {
        $attr = $producer->attributes();
        $producers[$i]['id'] = (int)$attr[0];
        $producers[$i]['name'] = (string)$producer->name;
        $producers[$i]['address'] = (string)$producer->address;
        $producers[$i]['zipcode'] = (string)$producer->zipcode;
        $producers[$i]['town'] = (string)$producer->towbn;
        $producers[$i]['url'] = (string)$producer->url;
        $producers[$i]['imgurl'] = (string)$producer->imgurl;
    
        $i += 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to save data from my database into xml file. I am using
I want to save data to xml file , which include Create xml file
I want to save the data from a certain link in a xml file
i want to store data from a XML file to the mysql am using
I have the next code to retrieve some data from a xml file. The
I am using jQuery to take data from an xml file and save it
I want to save user generated data in an XML file in the iphone.
I am trying to extract data from XML file and save it my C#
I don't want to overwrite the data in XML File i want to save
I want to save some custom data into application configuration file and I need

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.