A
A
Angelxalfa2014-12-06 17:45:15
PHP
Angelxalfa, 2014-12-06 17:45:15

How to export data from XML to MySQL database?

There is an XML file containing about 100,000 lines updated once a month. You need to enter the data from this file into the database table of the site on MySQL.
A piece of XML file:

<?xml version="1.0" encoding="UTF-8"?>
<document>
  <device_types>
    <type unit="1" name="567" id="1"/>
  </device_types>
  <location name="1">
    <street name="street">
      <house number="72А">
        <apartment number="3">
          <account id="1401160037" person="person">
            <device id="168576" value="157" date="2014-10-23 19:32:19" type="1"/>
            <device id="168716" value="30" date="2014-10-23 19:32:19" type="1"/>
          </account>
        </apartment>
        <apartment number="36">
          <account id="1401760040" person="person">
            <device id="158675" value="37" date="2014-10-24 17:33:43" type="1"/>
            <device id="164798" value="45" date="2014-10-24 17:33:43" type="1"/>
          </account>
        </apartment>
        <apartment number="48">
          <account id="1410760030" person="person">
            <device id="318922" value="11" date="2014-10-25 17:39:36" type="1"/>
            <device id="318977" value="39" date="2014-10-25 17:39:36" type="1"/>
          </account>
        </apartment>
      </house>
  </street>
    <street name="street">
      <house number="7">
        <apartment number="60">
          <account id="1503502005" person="person">
            <device id="263225" value="174" date="2014-10-23 08:23:51" type="1"/>
          </account>
        </apartment>
      </house>
      <house number="5">
        <apartment number="25">
          <account id="1503180057" person="person">
            <device id="170170" value="370" date="2014-10-23 19:01:35" type="1"/>
          </account>
        </apartment>
      </house>
    </street>
    <street name="street">
      <house number="1">
        <apartment number="90">
          <account id="1602800071" person="person">
            <device id="31210592" value="12" date="2014-10-23 09:17:16" type="1"/>
            <device id="312786" value="10" date="2014-10-23 09:17:16" type="1"/>
          </account>
        </apartment>
      </house>
      <house number="2">
        <apartment number="50">
          <account id="1602800033" person="person">
            <device id="2106610" value="119" date="2014-10-23 15:36:39" type="1"/>
          </account>
        </apartment>
        <apartment number="65">
          <account id="1602801017" person="person">
            <device id="227470" value="52" date="2014-10-25 17:09:02" type="1"/>
          </account>
        </apartment>
        <apartment number="35">
          <account id="1600170081" person="person">
            <device id="10130061225706" value="15" date="2014-10-25 19:08:11" type="1"/>
            <device id="10136134203" value="60" date="2014-10-25 19:08:11" type="1"/>
          </account>
        </apartment>
      </house>
    </street>
  </location>
</document>

I'm trying to parse a file using SimpleXML with this code:
<?php 
    $xmlURL = "xml.xml";

    $sxml = simplexml_load_file($xmlURL);
   
    foreach($sxml->location->street as $street) {
        foreach($sxml->location->street->house as $house) {
          $house_number = stripslashes($house->apartment->attributes()['number']);
            echo "<br>'$house_number'";
            $app_number = stripslashes($house->attributes()['number']);
            echo "_'$app_number'";
          $id = ($house->apartment->account->attributes()['id']);
            echo "_'$id'";
        }     
    }
?>

As a result of parsing, data is obtained only from the first element of the Street attachment and the same data is repeated as many times as there are street elements, that is, from the attached piece of xml:
'3'_'72Рђ'_'1401160037'
'3' _'72Рђ'_'1401160037'
'3'_'72Рђ'_'1401160037'
And I need the data to be taken from all elements. Please tell me how can I do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evervess, 2014-12-06
@Angelxalfa

I have never worked with SimpleXML, but according to the general logic, it should be like this

foreach($sxml->location->street as $street) {
        foreach($street->house as $house) { //Перебираем дома только на текущей улице
          $house_number = stripslashes($house->apartment->attributes()['number']);
            echo "<br>'$house_number'";
            $app_number = stripslashes($house->attributes()['number']);
            echo "_'$app_number'";
          $id = ($house->apartment->account->attributes()['id']);
            echo "_'$id'";
        }     
    }

E
Evgeny Kumanin, 2014-12-06
@jackkum

That's how it should be

foreach($sxml->location->street as $street) {
    foreach($street->house as $house) {
        // ...
    } 
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question