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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:33:39+00:00 2026-05-23T14:33:39+00:00

I have 2 questions: 1-I need to parse XML file and insert the data

  • 0

I have 2 questions:

1-I need to parse XML file and insert the data into mysql database. Let’s say that the file is around 250 kB (but it could be even larger) and it has a lot of subnodes, so I need at least 3 tables. I parsed the xml with SimpleXml and successfully inserted all the data into db. But for this exact file it took around 160s which seems to me a lot.
Is there a method to do better, in lesser time?

And another question is that I need to get the XML file from an URL and save to the server, and I’m not sure how to do this …

Thanks for your answers.

The code for parsing xml

function parse_xml($file=""){
  global $database;
  if(file_exists($file) && !empty($file)){
      $sport = new SimpleXMLElement($file, null, true);    
      $count = count($sport->OddsObject)-1;
      $listAttr = array();
      $start_time = time();
      for($i=0; $i <= $count; $i++){
          $countMatch = count($sport->OddsObject[$i]->Matches->Match)-1;
          //echo $countMatch; 
          for($k=0; $k <= $countMatch; $k++){           
              $OOdata = $sport->OddsObject[$i]->children();
              $columns = array();
              $data = array();
              foreach($OOdata as $key => $value){            
                  if($key != "Matches"){
                      //$listAttr[$i][$key] = $attr;
                      $columns[] = $key;
                      if ($value != "") {
                          $data[] = "'" . $database->escape_value($value) . "'";
                    } else {
                         $data[] = "NULL";
                    }
                }
            }        

            //get matches: MatchId, Date, HomeTeam, AwayTeam
            $Mdata = $sport->OddsObject[$i]->Matches->Match[$k]->children();     
            foreach ( $Mdata as $key => $value) {
                if($key != "OddsData"){    
                    $columns[] = $key;
                    if ($value != "") {
                      $data[] = "'" . $database->escape_value($value) . "'";
                    } else {
                      $data[] = "NULL";
                    }    
                }
            }                      
            $cols = strtolower(implode(",",$columns));
            $values = implode(",",$data);
            $sql = "INSERT INTO sports($cols) values(".$values.")";
            if($database->query($sql)) {
                $last_id = $database->insert_id();

                $countData = count($sport->OddsObject[$i]->Matches->Match[$k]->OddsData)-1;
                for($t=0; $t <= $countData; $t++){
                    //get OddsData: Home-,Draw-, -Away ...
                    $ODdata = $sport->OddsObject[$i]->Matches->Match[$k]->OddsData[$t]->children();
                    foreach($ODdata as $key=>$attr){
                        $MID = $last_id;
                        $new_bet = Bet::make($attr->getName(),$attr, $MID);
                        $new_bet->save(); 

                    }                    
                }
            }
        }
        $end_time = time() - $start_time;
    }    
    return $end_time;
}
else{
    die("The file doesn't exist.");
}
}
  • 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-23T14:33:39+00:00Added an answer on May 23, 2026 at 2:33 pm

    A pretty easy way to get a file from a url and write it is file_get_contents() and file_put_contents().

    SimpleXML should be pretty efficient and fast on a file thats only 250kb. Your slowness might be with your database inserts. Try grouping your inserts to the database. I’ve found that running 50 inserts at a time usually works the best(this depends on the row size though). That will probably speed up the whole process quite a bit.

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

Sidebar

Related Questions

I have a very large XML file that I need to parse so I
I've got a fairly big XML file that I need to parse into a
I have XML files that are representation of a portion of HTML code. Those
I am working on a wrapper that parses a data file for an existing
I have a huge XML files up to 1-2gb, and obviously I can't parse
I have problem with my Java program. How I read xml -file which has
I have an xml document with the following format: <response username=123 customerName=CustomerName siteName=SiteName customerID=123
I have found many questions here on SO and articles all over the internet
I have an odd bug where my code returns a file not found exception
I am using an API (Let's pretend its facebook) to gather data between two

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.