テキストファイルに1つの文字列があります。
このファイルからテキストを取得して、xml 属性値にプッシュしようとしています。
これは私のコードです:-
<?php
$region = file_get_contents('activity.text');
$arr1 = explode(',',$region);
$arr1[array_rand($arr1)];
$ran1 = $arr1;
// 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,$j=351;$i<count($arr1);$i++,$j++)
for($i=0;$i<2;$i++)
{
$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);
// create child dist_region
$dist_region = $dom->createElement("dist_region");
$pro_id->appendChild($dist_region);
// create attribute node
$attreg = $dom->createAttribute("value");
$dist_region->appendChild($attreg);
// create attribute value node
$attval = $dom->createTextNode("$ran1[$i]");
$attreg->appendChild($attval);
}
echo $dom->saveXML();
?>
これは私の文字列 (region.txt) です:-
10066,10067,10068,10069,10070,10071,10072,5
ここで、テキストを文字列から xml ファイルに属性値としてランダムにプッシュしようとしています。このコード出力は次のとおりです。
<products>
<product_id value="1">
<dist_activity value="10066"/>
<dist_activity value="10066"/>
</product_id>
<product_id value="2">
<dist_activity value="10067"/>
<dist_activity value="10067"/>
</product_id>
</products>
私はこのタイプのようなものを出力したい:-
<?xml version="1.0"?>
<products>
<product_id value="1">
<dist_activity value="10066"/>
<dist_activity value="10069(any thing not same)"/>
</product_id>
<product_id value="2">
<dist_activity value="10067"/>
<dist_activity value="10072(any thing not same)"/>
</product_id>
</products>
文字列から属性値をランダムに取得し
ます