0

I'm in an internship in a start up and I really need some of your help, after looking and thinking about my problem i can't figure a solution on how to go through it. I want to translate a xml file into a php array so that i can insert my datas into a SQL Database. (this will allow us to control some things easily)

Here is what my xml looks like:

 <?xml version="1.0" encoding="UTF-8"?>
<a att1="aa" att2="ab" att3="ac" att8="ad">
    <b>value</b>
    <c>
       <d>value</d>
       <d>value</d>
    </c>
    <e>value</e>
    <f att1="ae" att2="af">value</f>
    <f att1="ag">value</f>
    <g att1="ah" att5="ai">
        <h att1="aj">
           <i>value</i>
           <j>value</j>
        </h>
        <k att1="ak">value</k>
        <l att1="al">value</l>
        <m att1="am">value</m>
        <n att1="an">value</n>
    </g>
    <o att1="ao" att5="ap">
        <h att1="aq">
           <i>value</i>
           <j>value</j>
        </h>
        <k att1="ar">value</k>
        <l att1="as">value</l>
        <m att1="at">value</m>
        <n att1="au">value</n>
    </o>
    <p>
        <q att1="av">value</q>
        <r att1="aw">value</r>
        <s att1="ax">value</s>
        <t att1="ay">value</t>
    </p>
    <u>
        <v>value</v>
    </u>
    <w>value</w>

I tried a lot of ways i found on the web, but so far the most efficient was

$xml = file_get_contents($fileName);
$ob = simplexml_load_string($xml);
$json = json_encode($ob);
$configData = json_decode($json, true);
... and here i treat my php array

The problem of this code and of all those i found on stack overflow (the answers from Parsing Huge XML Files for example)is that a lot of the attributes are lost during the process and so i can't have a ll the informations we need in the database.

If someone have an idea on how to keep all the attributes and transform this looks a like xml into a usable array i ask on my knee for him to help me, because i'm really getting mad on this :o

Thanks in advance!

Palencar
  • 159
  • 12
  • Try this: https://stackoverflow.com/questions/911663/parsing-huge-xml-files-in-php – Billu Jul 13 '17 at 08:27
  • 1
    https://stackoverflow.com/questions/1835177/how-to-use-xmlreader-in-php – mim. Jul 13 '17 at 08:29
  • The attributes are not lost. `json_encode()` stores them as array into the property named `@attributes` of the encoded object. `json_decode()` restores them correctly. – axiac Jul 13 '17 at 08:31
  • @axiac Some attributes are in tabs, others desappear, for example the `xsi:att1` in `` and `` isn't in the `@attribute` tab, and the `` and `` tags don't even have a `@attribute` :/ – Palencar Jul 13 '17 at 08:37
  • I couldn't test with the fragment you posted; it is not valid. Please post a valid fragment of XML to expose the behaviour. – axiac Jul 13 '17 at 08:38
  • @Billu G On my way! – Palencar Jul 13 '17 at 08:39
  • @axiac I don't have the right to use the real datas (sensible informations) that's why i writted the xml like that :c – Palencar Jul 13 '17 at 08:40
  • You didn't get my point. I don't care about your real data. It can be only "foo" and "bar" all over. The text you posted is not XML. It cannot be parsed by any XML parsing code. I tried with a valid XML and it worked fine. If **you** care about getting an answer then make an effort and post a valid XML than can be used to develop a solution. – axiac Jul 13 '17 at 08:55
  • @axiac wow, ok i didn't understand your point at first, does this new one work (i checked it and i got no errors returned) – Palencar Jul 13 '17 at 09:05
  • 1
    Possible duplicate of [Parsing Huge XML Files in PHP](https://stackoverflow.com/questions/911663/parsing-huge-xml-files-in-php) – Pavel Janicek Jul 13 '17 at 09:07
  • @PavelJanicek I already tried the solutions proposed on this link and i have the exact same problem, some of the attributes are lost...:/ – Palencar Jul 13 '17 at 09:34

0 Answers0