douxuanou2787 2013-05-27 06:44
浏览 20
已采纳

在单个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...

  • 写回答

1条回答 默认 最新

  • doubi1624 2013-05-27 07:18
    关注

    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());
     }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 三菱FX系列PLC串口指令
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型