title: Need to use php foreach to create XML elements and attributes using a second XML
body: <p>I have an XML doc (<code>traffic.xml</code>) that I then used PHP to pull only 2 specific attributes using foreach as there is a ton of them in the original XML to create a PHP(<code>drivetimeFetch.php</code>). </p>
  
  <p>What I need to do now is, using <code>drivetimeFetch.php</code>, I need to create another xml doc (<code>carousel_Traffic.xml</code>) with elements and attributes made up of the 30 or so lines from the stripped <code>drivetimeFetch.php</code> I created. Here is where I'm getting stuck. I was able to write the elements and attributes but how do I get each line into its own element block. </p>
  
  <p>Here is what my php looks like:</p>
  
  <pre><code>&lt;?php
  $xml1 = new SimpleXMLElement(file_get_contents('https://wsidata.weather.com/201303/en-us/55828880/traffic.xml'));
  foreach ($xml1-&gt;drivetimes-&gt;drivetime as $drivetime) {
      echo"&lt;br&gt;";
      echo $drivetime['pathName'].":"." ".$drivetime['driveTimeMinutes']."min \n" ;
  }
  $xml_txt = '&lt;?xml version="1.0" encoding="UTF-8"?&gt;'."\r\n".'&lt;tickerfeed version="2.4"&gt;'."\r\n". 
             '&lt;playlist type="scrolling_carousel" name="TrafficScroll" target="carousel"&gt;'."\r\n" ;
  $element = '&lt;element&gt;';
  $elementClose = '&lt;/element&gt;';
  $field1 = '&lt;field name="05"&gt;';           
  $fieldClose = '&lt;/field&gt;';
  $playlistClose = '&lt;/playlist&gt;';
  $tickerfeedClose = '&lt;/tickerfeed&gt;';
  
  $f = fopen("C:\\Users\\Administrator\\Desktop\\carousel_Traffic.xml", "w"); 
  fwrite ($f, $xml_txt. "\r\n" );
  fwrite ($f, $element. "\r\n" );
  fwrite ($f, $field1. $drivetime['pathName'].":"." ".$drivetime['driveTimeMinutes']."min " . $fieldClose. "\r\n" );
  fwrite ($f, $elementClose. "\r\n" );
  fwrite ($f, $playlistClose. "\r\n" );
  fwrite ($f, $tickerfeedClose. "\r\n" );
  fclose ($f);
  ?&gt;
  </code></pre>
  
  <p>What I need from this is to get that carousel_Traffic.xml to look like is this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;tickerfeed version="2.4"&gt;
  &lt;playlist type="scrolling_carousel" name="TrafficScroll" target="carousel"&gt;
  
  &lt;element&gt;
  &lt;field name="05"&gt;I-278: Bklyn Battery Tunnel to Brooklyn Bridge: 10min &lt;/field&gt;
  &lt;/element&gt;
  &lt;/playlist&gt;
  &lt;/tickerfeed&gt;
  </code></pre>
  
  <p>For every one of those lines in the <code>drivetimeFetch.php</code>, I'm guessing this is a foreach operation but I have no idea how to do that so that the <code>pathName</code> and <code>driveTimeMinutes</code> for each line gets its own element block. As you can see, I got it to work for only one line.</p>