0

please, I have this code to import data from XML to database:

$a = glob('data/*/*.xml');

echo "import kategorie ...... ";
foreach ($a as $i) {
    $xml = simplexml_load_file("$i") or die ("Chyba: Nemuzu nacist soubor");

    foreach($xml->DocumentElement as $entry) {
        foreach ($entry->hotel as $dataHotel) {
            addCategory("$dataHotel->country", "$dataHotel->location", "$dataHotel->location2");
        }
        foreach ($entry->Popisy as $dataPopisy) {
            addHotel("$dataHotel->hotel", "$dataPopisy->doporuc");
        }
    }
}
echo "OK\n"

I can not figure out how to do it - I need in function "addHotel" get data from "hotel" array and from "Popisy" array. So, I need to get from two at once.

Here is XML structure: http://pastebin.com/TNTpBijg and here http://fmnet.cz/HLS240.xml

Is this possilbe? Thank you very much!


Now I tried this:

$a = glob('data/*/*.xml');

echo "import kategorie ...... ";
foreach ($a as $i) {
    $xml = simplexml_load_file("$i") or die ("Chyba: Nemuzu nacist soubor");

    foreach($xml->DocumentElement as $entry) {
        foreach ($entry->hotel as $dataHotel) {
            //addCategory("$dataHotel->country", "$dataHotel->location", "$dataHotel->location2");
            foreach ($entry->Popisy as $dataPopisy) {
               //addHotel("$dataHotel->hotel", "$dataPopisy->doporuc");
                echo "$dataHotel->hotel";
                echo "\n";
                echo "$dataPopisy->doporuc";
                echo "\n";
            }
        }
    }
}
echo "OK\n";

but output is only: import kategorie ...... OK

4
  • How do you decide which hotel goes with which Popisy? Can you show us a sample of the data, to see the format? Commented May 22, 2014 at 8:42
  • here is XML structure: pastebin.com/TNTpBijg Commented May 22, 2014 at 8:45
  • In the above link you have a php dump of the SimpleXML variable. Can you show us an example of the raw xml? Commented May 22, 2014 at 8:55
  • no problem: fmnet.cz/HLS240.xml thanks Commented May 22, 2014 at 9:20

1 Answer 1

1

Yes, it is possible. You can do it by embedding the popisy loop inside the hotel loop or the other way round. Check the code below:

$a = glob('data/*/*.xml');

echo "import kategorie ...... ";
foreach ($a as $i) {
    $xml = simplexml_load_file("$i") or die ("Chyba: Nemuzu nacist soubor");

    foreach($xml->DocumentElement as $entry) {
        foreach ($entry->hotel as $dataHotel) {
            addCategory("$dataHotel->country", "$dataHotel->location", "$dataHotel->location2");
            foreach ($entry->Popisy as $dataPopisy) {
               addHotel("$dataHotel->hotel", "$dataPopisy->doporuc");
            }
        }
    }
}
echo "OK\n"
Sign up to request clarification or add additional context in comments.

1 Comment

I treid this method but not working, when I try: in foreach($entry->Popisy....) run echo "$dataHotel->hotel"; or echo "$dataPopisy->doporuc"; echo not working, no output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.