How to parse xml file into mysql table using php?

  • 0
    I'm not good friends with php, so I ask for help.
    The structure of the xml file looks like this:

    <column name="id">...</column> 
      <column name="title">....</column> 
      <column name="parent">95</column> 
      <column name="id_group">50</column> 
      <column name="hide">0</column> 
      <column name="parent_field">13</column> 
      <column name="filename">...</column> 
      <column name="isfilename">1</column> 
      <column name="id_rmapo">...</column> 
      <column name="cycle_students">...</column> 
      <column name="cycle_type">..</column> 
      <column name="cycle_money" /> 
      <column name="cycle_cert">0</column> 
      <column name="cycle_employment">.</> 
      <column name="cycle_duration">1</column> 
      <column name="cycle_from">...</column> 
      <column name="cycle_to">....</column> 
      <column name="cycle_quantity">..</column> 
      <column name="cycle_year">2017</column>

    The table itself
    MySQL Anonymous, Sep 7, 2019

  • 1 Answers
  • 0
    An approximate algorithm does not take into account your system, connection to the database, etc.:
    1. Get the contents of a file, if it is on disk then file_get_contents or fread
    2. Parse an xml document, so that you can work with him. For example, using SimpleXMLElement
    3. In a loop, for example, foreach, all root elements and in each iteration add an entry to the base.

    This is the easiest option. If there are a lot of records in the file, it is necessary in step 3 to collect requests for n requests and insert them into the database in batches. But this is out of necessity
    Isla Francis

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!