在单个txt文件中获取product_id

时间:2013-05-27 06:44:52

标签: php xml

我尝试创建一个xml树。 类似这样的东西: -

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

我尝试将它们保存到一个txt文件中: - 比如1.txt

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


<{1}}

中的

2.txt

我试过这个: -

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

它的回报输出如下: - 在<?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(); ?>

1.txt


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

2.txt


我希望改变什么来获取diff文件中的所有product_id ...
感谢...

1 个答案:

答案 0 :(得分:3)

此代码可根据您的要求使用

<?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());
 }
?>
相关问题