0

I want to save the array below ( "name: $array" ), in an xml file using XMLDOM. I tried with only one city and it works well. But when integrating the code to multiple array. it fails.

The

Array ( [0] => Array ( [city] => Array ( [0] => Reduit [1] => Curepipe )

        [distance] =>40
    )

[1] => Array
    (
        [city] => Array
            (
                [0] => Array
                    (
                        [0] => Reduit
                        [1] => Ebe
                    )

                [1] => Bees Village
                [2] => Phoen Trunk Rd
                [3] => Riv,Phoenix
                [4] => St l Rd
                [5] => Geoes Guibert St
                [6] => Curepipe
            )

        [distance] => 20        )

[2] => Array
    (
        [city] => Array
            (
                [0] => Array
                    (
                        [0] => Reduit
                        [1] => Riv,Phoenix
                    )

                [1] => St l Rd
                [2] => Geoes Guibert St
                [3] => Curepipe
            )

        [distance] =>155
    )

[3] => Array
    (
        [city] => Array
            (
                [0] => Array
                    (
                        [0] => Reduit
                        [1] => Ebene
                    )

                [1] => Belles Village
                [2] => Phoenix Trunk Rd
                [3] => Riverside,Phoenix
                [4] => St Paul Rd
                [5] => Georges Guibert St
                [6] => Curepipe
            )

        [distance] => 79
    )

)

Here my working

function saveToXml($flatArray,$flat){

   #create a domdocument

   $domDocument = new DOMDocument('1.0','utf-8');

   $domDocument->formatOutput = true;

   $domDocument->load('result.xml');

   $xpath = new DOMXPath($domDocument);   

   $results = $xpath->query('/mauritius/pair');

   $newItem = $results->item(0);

   #get length of city in file

   $lengthCity =  $domDocument->getElementsByTagName('city')->length;

   for($i=0;$i<$lengthCity;$i++){

  #check if city exist

  if ($lengthCity >0 ){

         #delete all city

         echo $lengthCity;

         foreach ($results as $result){

            $city=$result->getElementsByTagName('city')->item(0);

            $result->removeChild($city);

         }
  }
}

#loop through all values

for ($row=0; $row<$flatArray;$row++){

#addElement

$new_node = $domDocument->createElement('city');

$text_node = $domDocument->createTextNode($flat[$row]);

$new_node->appendChild($text_node);

$newItem->appendChild($new_node);

}

echo $domDocument->save('result.xml');

} 

is it possible to convert the array into xml ?

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133

1 Answers1

0

A Similar answer was given here which includes a function to convert the array to XML, and could easily be adapted to what you're trying to do, I think.

Community
  • 1
  • 1
GDP
  • 8,109
  • 6
  • 45
  • 82
  • yes but i my only problem is how to save the nested arrays without flattening the result and merge everything. – user1419210 May 27 '12 at 15:56
  • Guess I don't understand the question full then, I've used the 2nd example on that answer, and it yielded the full depth of the array into a SimpleXML object, from which I saved it to a file. – GDP May 27 '12 at 16:03
  • Don't know what else to add to the extensive discussion in that answer then - I have always found the solution there - perhaps data types in your array. – GDP May 27 '12 at 16:23
  • @user1419210 you said it didn't work ? how come there are 40 vote ups for that post and it didn't work? Provide us the errors you get ... – HamZa May 27 '12 at 17:11
  • Notice: Undefined variable: student in C:\Users\zero\Desktop\EasyPHP-5.3.8.0\www\mtns_server\dijkstraAlgorithm.php on line 212 Warning: Invalid argument supplied for foreach() in C:\Users\zero\Desktop\EasyPHP-5.3.8.0\www\mtns_server\...php on line 218 – user1419210 May 27 '12 at 17:24
  • can you supply an example please ? – user1419210 May 27 '12 at 17:25