在单个txt文件中获取product_id

i am try to create one xml tree. something like this type:-

<products>
  <product_id value='1' />
</products>

and i am try to save them into one txt file:- like 1.txt

<products>
   <product_id value='1' />
</products>


in 2.txt

<products>
  <product_id value='2' />
</products>

i tried this:-

<?php
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text 
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("products");
$dom->appendChild($root);
for($i=0;$i<2;$i++)
{
//create product_id
$pro_id = $dom->createElement("product_id");
$root -> appendChild($pro_id);
//append attribute ib product_id
$attpro = $dom->createAttribute("value");
$pro_id -> appendChild($attpro);
//append attribue value in product_id
$attval = $dom->createTextNode($i+1);
$attpro -> appendChild($attval);
$a = $i+1;
file_put_contents("$a.txt",$dom->saveXML());
}
echo $dom->saveXML();
?>

its return me output like:- in 1.txt

 <products>
      <product_id value='1' />
    </products>


in 2.txt

<products>
      <product_id value='1' />
    </products>
 <products>
      <product_id value='2' />
    </products>


what i want to change for getting all product_id in diff file...
thanks...

This code will work as per your requirement

<?php

for($i=0;$i<2;$i++)
{
   $dom = new DOMDocument("1.0");

   header("Content-Type: text/plain");
   $root = $dom->createElement("products");
   $dom->appendChild($root);
   $pro_id = $dom->createElement("product_id");
   $root -> appendChild($pro_id);
   $attpro = $dom->createAttribute("value");
   $pro_id -> appendChild($attpro);
   $attval = $dom->createTextNode($i+1);
   $attpro -> appendChild($attval);
   $a = $i+1;
   file_put_contents("`enter code here`$a.txt",$dom->saveXML());
 }
?>