Create XML using DOMDocument class

Sometimes we need to create complex xml files/output. With nested tags.
To do this we can use the native PHP class DOMDocument.
Here is how I used this class to create an XML for a store finder functionality.



  $dom = new DOMDocument("1.0");
  $node = $dom->createElement('markers');
  $parnode = $dom->appendChild($node);
  header("Content-type: text/xml");
  foreach($xml_array as $key => $row){
    $node = $dom->createElement('marker');
    $newnode = $parnode->appendChild($node);
    $newnode->setAttribute("name", $row['name']);
    $newnode->setAttribute("lat", $row['lat']);
    $newnode->setAttribute("lon", $row['lon']);
    $newnode->setAttribute("category", $row['category']);
    $newnode->setAttribute("address", $row['address']);
    $newnode->setAttribute("city", $row['city']);
    $newnode->setAttribute("postal", $row['postal']);
    $newnode->setAttribute("country", $row['country']);
    $newnode->setAttribute("phone", $row['phone']);
    $newnode->setAttribute("email", htmlentities($row['email']));

  }

This will create an XML that looks something like this:

<markers>
<marker address="3040 Excelsior Blvd" address2="" category="Restaurant" city="Minneapolis" country="US" email="info@chipotle.com" featured="" features="" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.947464" lng="-93.320826" name="Chipotle Minneapolis" phone="612-922-6662" postal="55416" state="MN" web="http://www.chipotle.com">
<marker address="5480 Excelsior Blvd." address2="" category="Restaurant" city="St. Louis Park" country="US" email="info@chipotle.com" featured="" features="Online Ordering " hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.930810" lng="-93.347877" name="Chipotle St. Louis Park" phone="952-922-1970" postal="55416" state="MN" web="http://www.chipotle.com">
<marker address="2600 Hennepin Ave." address2="" category="Restaurant, Bar" city="Minneapolis" country="US" email="info@chipotle.com" featured="true" features="Online Ordering, Fresh Guacamole" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.9553438" lng="-93.29719699999998" name="Chipotle Minneapolis" phone="612-377-6035" postal="55404" state="MN" web="http://www.chipotle.com">
<marker address="515 Winnetka Ave. N" address2="" category="Restaurant" city="Golden Valley" country="US" email="info@chipotle.com" featured="" features="Online Ordering" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.983935" lng="-93.380542" name="Chipotle Golden Valley" phone="763-544-2530" postal="55427" state="MN" web="http://www.chipotle.com">
<marker address="786 Mainstreet" address2="" category="Cafe" city="Hopkins" country="US" email="info@chipotle.com" featured="" features="Margaritas" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.924363" lng="-93.410158" name="Chipotle Hopkins" phone="952-935-0044" postal="55343" state="MN" web="http://www.chipotle.com">
<marker address="1040 Nicollet Ave" address2="" category="Restaurant" city="Minneapolis" country="US" email="info@chipotle.com" featured="" features="Online Ordering, Margaritas" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.973557" lng="-93.275111" name="Chipotle Minneapolis" phone="612-659-7955" postal="55403" state="MN" web="http://www.chipotle.com">
<marker address="50 South 6th" address2="" category="Restaurant, Cafe" city="Minneapolis" country="US" email="info@chipotle.com" featured="" features="Fresh Guacamole" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.97774" lng="-93.270909" name="Chipotle Minneapolis" phone="612-333-0434" postal="55402" state="MN" web="http://www.chipotle.com">
<marker address="6801 York Avenue South" address2="" category="Restaurant" city="Edina" country="US" email="info@chipotle.com" featured="" features="Online Ordering, Fresh Guacamole" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.879826" lng="-93.321280" name="Chipotle Edina" phone="952-926-6651" postal="55435" state="MN" web="http://www.chipotle.com">
<marker address="12509 Wayzata Blvd" address2="" category="Restaurant" city="Minnetonka" country="US" email="info@chipotle.com" featured="" features="Margaritas" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.970495" lng="-93.437430" name="Chipotle Minnetonka" phone="952-252-4900" postal="55305" state="MN" web="http://www.chipotle.com">
<marker address="229 Cedar Ave S" address2="" category="Restaurant, Coffee" city="Minneapolis" country="US" email="info@chipotle.com" featured="" features="" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.972808" lng="-93.247153" name="Chipotle Minneapolis" phone="612-659-7830" postal="55454" state="MN" web="http://www.chipotle.com">
<marker address="225 Hennepin Ave E" address2="" category="Restaurant" city="Minneapolis" country="US" email="info@chipotle.com" featured="" features="Fresh Guacamole" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.987687" lng="-93.257581" name="Chipotle Minneapolis" phone="612-331-6330" postal="55414" state="MN" web="http://www.chipotle.com">
<marker address="800 Washington Ave SE" address2="" category="Restaurant" city="Minneapolis" country="US" email="info@chipotle.com" featured="" features="" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.973665" lng="-93.227023" name="Chipotle Minneapolis" phone="612-378-7078" postal="55414" state="MN" web="http://www.chipotle.com">
<marker address="322 South Ave" address2="" category="Restaurant" city="Bloomington" country="US" email="info@chipotle.com" featured="" features="Online Ordering" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.8458631" lng="-93.2860161" name="Chipotle Bloomington" phone="952-252-3800" postal="55425" state="MN" web="http://www.chipotle.com">
<marker address="1313 Wayzata Blvd" address2="" category="Restaurant" city="Wayzata" country="US" email="info@chipotle.com" featured="" features="Margaritas" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.9716626" lng="-93.4967757" name="Chipotle Wayzata" phone="952-473-7100" postal="55391" state="MN" web="http://www.chipotle.com">
<marker address="13250 Technology Dr" address2="" category="Restaurant, Bar" city="Eden Prairie" country="US" email="info@chipotle.com" featured="" features="Online Ordering" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.859761" lng="-93.436379" name="Chipotle Eden Prairie" phone="952-934-5955" postal="55344" state="MN" web="http://www.chipotle.com">
<marker address="3425 Vicksburg Lane N" address2="" category="Restaurant" city="Plymouth" country="US" email="info@chipotle.com" featured="" features="Online Ordering, Fresh Guacamole" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="45.019846" lng="-93.481832" name="Chipotle Plymouth" phone="763-519-0063" postal="55447" state="MN" web="http://www.chipotle.com">
<marker address="860 Rosedale Center Plaza" address2="" category="Restaurant, Coffee" city="Roseville" country="US" email="info@chipotle.com" featured="" features="" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.998965" lng="-93.194622" name="Chipotle Roseville" phone="651-633-2300" postal="55113" state="MN" web="http://www.chipotle.com">
<marker address="867 Grand Ave" address2="" category="Restaurant, Bakery" city="St. Paul" country="US" email="info@chipotle.com" featured="" features="" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.939865" lng="-93.136768" name="Chipotle St. Paul" phone="651-602-0560" postal="55105" state="MN" web="http://www.chipotle.com">
<marker address="560 W 79th" address2="" category="Restaurant, Bar" city="Chanhassen" country="US" email="info@chipotle.com" featured="" features="Online Ordering, Fresh Guacamole" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.858736" lng="-93.533661" name="Chipotle Chanhassen" phone="952-294-0301" postal="55317" state="MN" web="http://www.chipotle.com">
<marker address="29 5th St West" address2="" category="Restaurant, Coffee" city="St. Paul" country="US" email="info@chipotle.com" featured="" features="Fresh Guacamole" hours1="Mon-Sun 11am-10pm" hours2="" hours3="" lat="44.945127" lng="-93.095368" name="Chipotle St. Paul" phone="651-291-5411" postal="55102" state="MN" web="http://www.chipotle.com">
</marker></marker></marker></marker></marker></marker></marker></marker></marker></marker></marker></marker></marker></marker></marker></marker></marker></marker></marker></marker></markers>

Add new comment