-1

I'm trying to load a foreach into a string formatted as XML.

I've tried as follows

  <?php
  $data = '<?xml version="1.0" encoding="utf-8" ?><markers>';
  foreach ($entries as $entry => $marker ) {
    '<marker name="' . $marker->getName() . '"'.'lat="' . $marker->getLatitude() . '"'.'lng="' . $marker->getLongitude() . '"'.'address="' . $marker->getAddressLineOne() . '"'.'address2="' . $marker->getAddressLineTwo() . '"'.'city="' . $marker->getCitySuburb() . '"'.'state="' . $marker->getState('fieldName') . '"'.'postal="' . $marker->getPostCode() . '"'.'country="' . $marker->getCountry('fieldName') . '"'.'phone="' . $marker->getPhone() . '"'.'email="' . $marker->getEmail() . '"'.'web="' . $marker->getWebSite() . '"'.'/>';
  }
  '</markers>';
  ?>

But what I end up getting is:

  1. nothing in the $data variable
  2. for some reason each item is nesting in the previous item

Basically I would like to achieve the following result:

<?php
$data = '<?xml version="1.0" encoding="utf-8"?>
<markers>
    <marker name="Chipotle Minneapolis" lat="44.947464" lng="-93.320826" category="Restaurant" address="3040 Excelsior Blvd" address2="" city="Minneapolis" state="MN" postal="55416" country="US" phone="612-922-6662" email="info@chipotle.com" web="http://www.chipotle.com" />
    <marker name="Chipotle St. Louis Park" lat="44.930810" lng="-93.347877" category="Restaurant" address="5480 Excelsior Blvd." address2="" city="St. Louis Park" state="MN" postal="55416" country="US" phone="952-922-1970" email="info@chipotle.com" web="http://www.chipotle.com" />
    <marker name="Chipotle Minneapolis" lat="44.9553438" lng="-93.29719699999998" category="Restaurant, Bar" address="2600 Hennepin Ave." address2="" city="Minneapolis" state="MN" postal="55404" country="US" phone="612-377-6035" email="info@chipotle.com" web="http://www.chipotle.com" />
</markers>';
?>

enter image description here

Thanks Said

Revised code


<?php

$data = simplexml_load_string("<markers />");
foreach ($entries as $entry => $marker ) {
    $newMarker = $data->addChild("marker");
    $newMarker->addAttribute("name", $marker->getName());
    $newMarker->addAttribute("lat", $marker->getLatitude());
    $newMarker->addAttribute("lng", $marker->getLongitude());
    $newMarker->addAttribute("state", $marker->getPostCode());
}

echo $data->asXML();

?>
  <?php
  echo var_dump($data);
  ?>
Said
  • 186
  • 2
  • 13

2 Answers2

0

After the first line where you use

$data = '<?xml version="1.0" encoding="utf-8" ?><markers>';

you don't add any of the further text onto the variable. You need to use something like

$data .='<marker name="' . $marker->getName() .` 

(with the dot before the = to add the value), you also need to be careful with the spacing and the quotes.

I would also recommend using something like SimpleXML to build the XML rather than using put text, it is longer code but safer. Something like...

$xml = simplexml_load_string("<markers />");
foreach ($entries as $entry => $marker ) {
    $newMarker = $xml->addChild("marker");
    $newMarker->addAttribute("name", $marker->getName());
    $newMarker->addAttribute("lat", $marker->getLatitude());
    $newMarker->addAttribute("lng", $marker->getLongitude());
    // Repeat for all attributes
}

echo $xml->asXML();
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Thanks Nigel for the response, appreciated, the XML seems to work but for some reason the marker is nesting in the previous marker (see added image) and not sure the $data is loading the same as when I do a var_dump($data); its returning the php array instead of xml. I added the revised code – Said Nov 01 '20 at 09:32
  • @Said, ignore `var_dump($data);`, the output should be from `echo $xml->asXML();`. With this code I can't reproduce the nested `` elements. – Nigel Ren Nov 01 '20 at 15:05
0

Since it's not clear to me how $entries are generated, I'll make up some data in couple of arrays and create a simplified version of your expected output, with the help of a function borrowed from here. Obviously, you'll need to modify it to fit your actual code.

$data = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<markers>
</markers> 
XML;

$xml = simplexml_load_string($data);

function sxml_append(SimpleXMLElement $to, SimpleXMLElement $from) {
    $toDom = dom_import_simplexml($to);
    $fromDom = dom_import_simplexml($from);
    $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}


$stores = ["Chipotle Chicago","Chipotle St. Louis Park","Chipotle Minneapolis"];
$locations = ["Chicago","St. Louis Park","Minneapolis"];
$destination = $xml->xpath('//markers');

foreach(array_combine($stores, $locations) as $store => $location) {
$tm = simplexml_load_string('<marker name="xxx" city="yyy"/>');
$tmxml = $tm->xpath('//marker')[0];
$tmxml['name'] = $store;
$tmxml['city']  = $location;
sxml_append($destination[0], $tmxml);
}

echo $xml->asXML();

Output:

    <?xml version="1.0" encoding="UTF-8"?>
<markers>
   <marker name="Chipotle Chicago" city="Chicago" />
   <marker name="Chipotle St. Louis Park" city="St. Louis Park" />
   <marker name="Chipotle Minneapolis" city="Minneapolis" />
</markers>
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45