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>"."
";
foreach($item as $_item) {
echo $xml .= "<city CityId=$quote" . $_item['CityId'] . "$quote StateId=$quote" . $_item['StateId'] . "$quote CityName=$quote" . $_item['CityName'] . "$quote />
";
}
$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.