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.

5
  • Have you read stackoverflow.com/questions/1397036/… ? It should give you something to start with. Commented Feb 25, 2014 at 9:52
  • Yes, i read that post, the format is different. Commented Feb 25, 2014 at 9:54
  • My issue is that im getting only 1 row instead 10, 100, or how many are in the array. Commented Feb 25, 2014 at 9: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. Commented Feb 25, 2014 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. Commented Feb 25, 2014 at 10:06

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

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
Quoted from stackoverflow.com/questions/1191167/… : You can use dom_import_simplexml($xml)->ownerDocument. Then set $dom->formatOutput = true and you can get the result via $dom->saveXML()
-1

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

1 Comment

Hi @Joseph, is not the right format there. Thanks a lot.

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.