0

I'm trying to get an xml file from a foreach loop from an array but im getting only 1 row in the xml file.

Is there another way to convert the array data to xml file in this format?

PHP:

$item = array();
        $item[] = array('CityId'    => $row['city_id'],
                        'StateId'   => $row['state_id'],
                        'CityName'  => $row['city_name']
                        );

        $break = "<br />";
        $quote = '"';
        $xml = "<data>"."\n";
        foreach($item as $_item) {

            echo $xml .= "<city CityId=$quote" . $_item['CityId'] . "$quote StateId=$quote" . $_item['StateId'] . "$quote CityName=$quote" . $_item['CityName'] . "$quote />\n";

        }
        $xml .= "</data>";
        $sxml = new SimpleXMLElement($xml);
        echo $output = $sxml->asXML("data.xml");

Output data.xml:

<data>
<city CityId="1" StateId="217" CityName="New York" />
<city CityId="2" StateId="218" CityName="New Jersey" />
</data>

Any help is appreciated.

Dario
  • 704
  • 2
  • 21
  • 40
  • Have you read http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml ? It should give you something to start with. – Brice Feb 25 '14 at 09:52
  • Yes, i read that post, the format is different. – Dario Feb 25 '14 at 09:54
  • My issue is that im getting only 1 row instead 10, 100, or how many are in the array. – Dario Feb 25 '14 at 09:56
  • A side note, since you're already using SimpleXML, you can use it all the way trough (`$city=$xml->addChild("city");$city->addAttribute("CityId","1");...`), instead of caching a whole string and then convert it to SimpleXML. – Passerby Feb 25 '14 at 10:03
  • I suppose `$row` is coming fram a database? Looks like your not looping trough the results from your query. Add `print_r($item,true)` before your foreach and see how many items the array has. – Michel Feb 25 '14 at 10:06

2 Answers2

1

Data :

 $cities = array(
        array(
            'cityId' => 1,
            'stateId' => 217,
            'cityName' => 'New York'
        ),
        array(
            'cityId' => 2,
            'stateId' => 0,
            'cityName' => 'Paris'
        ),
    );

This :

$xml = new SimpleXMLElement('<data/>');
foreach ($cities as $key => $city)
{
    $xml->city[$key]['cityId'] = $city['cityId'];
    $xml->city[$key]['stateId'] = $city['stateId'];
    $xml->city[$key]['cityName'] = $city['cityName'];
}

OR (better) :

xml = new SimpleXMLElement('<data/>');
foreach ($cities as $cityData)
{
    $city = $xml->addChild('city');
    $city->addAttribute('cityId', $cityData['cityId']);
    $city->addAttribute('stateId', $cityData['stateId']);
    $city->addAttribute('cityName', $cityData['cityName']);
}

Will output :

<data>
    <city cityId="1" stateId="217" cityName="New York" />
    <city cityId="2" stateId="0" cityName="Paris" />
</data>
Brice
  • 1,026
  • 9
  • 20
  • Hello Brice, this is what i need, can you please tell me how can i format the outpup xml? i mean the output is all inline. Thx a lot – Dario Feb 25 '14 at 10:17
  • Quoted from http://stackoverflow.com/questions/1191167/format-output-of-simplexml-asxml : You can use `dom_import_simplexml($xml)->ownerDocument`. Then set `$dom->formatOutput = true` and you can get the result via `$dom->saveXML()` – Brice Feb 25 '14 at 10:24
0

have a look here. i have been using this function to convert array to xml. hope it will help you.

http://vantulder.net/old-articles/array-to-xml

Joe
  • 618
  • 1
  • 9
  • 18