xml - How to PHP DomDocument creating attributes? -
xml - How to PHP DomDocument creating attributes? -
i've been smacking head against wall 2 days trying figure out how php encode xml want. tried simplexml , found out there serious limitations, using domdocument fulfill needs. problem quite basic, proper syntax?
i retrieving code database, rendering xml. xml construction has in same exact format 1 going post. issue when comes attributes. output has 3 attributes need repeated twelve times different values. problem trying figure out how render attributes, code necessary.
here xml:
<inits> <version>18.05.04_ep1</version> <source>live</source> <lowid>265067</lowid> <highid>265068</highid> <ql>300</ql> <name>ofab shark mk 1</name> <inits slider="def>===========][<agg" percent="100" init="430" /> <inits slider="def>==========][=<agg" percent="90" init="530" /> <inits slider="def>=========][==<agg" percent="81" init="630" /> <inits slider="def>========][===<agg" percent="72" init="730" /> <inits slider="def>=======][====<agg" percent="63" init="830" /> <inits slider="def>======][=====<agg" percent="54" init="930" /> <inits slider="def>=====][======<agg" percent="45" init="1030" /> <inits slider="def>====][=======<agg" percent="36" init="1130" /> <inits slider="def>===][========<agg" percent="27" init="1290" /> <inits slider="def>==][=========<agg" percent="18" init="1590" /> <inits slider="def>=][==========<agg" percent="9" init="1890" /> <inits slider="def>][===========<agg" percent="0" init="2190" /> </inits>
notice inits contains attributes, percent, , init. going display 12 times in example, derived info , php calculations. here code using far. note: skipping info , calculation functions , filling in info manually.
$root = $doc->createelement('inits'); $root = $doc->appendchild($root); $version = $doc->createelement('version'); $version = $root->appendchild($version); $versiontext = $doc->createtextnode($patchnum); $versiontext = $version->appendchild($versiontext); $source = $doc->createelement('source'); $source = $root->appendchild($source); $sourcetext = $doc->createtextnode('live'); $sourcetext = $source->appendchild($sourcetext); $xlowid = $doc->createelement('lowid'); $xlowid = $root->appendchild($xlowid); $xlowidtext = $doc->createtextnode($lowid); $xlowidtext = $xlowid->appendchild($xlowidtext); $xhighid = $doc->createelement('highid'); $xhighid = $root->appendchild($xhighid); $xhighidtext = $doc->createtextnode($highid); $xhighidtext = $xhighid->appendchild($xhighidtext); $xql = $doc->createelement('ql'); $xql = $root->appendchild($xql); $xqltext = $doc->createtextnode($ql); $xqltext = $xql->appendchild($xqltext);
where go here 3 attributes work, xml illustration above. give thanks you.
to set attribute, utilize $some_node->setattribute("name","value")
. repeat needed attributes.
also, note can chain function calls:
$root = $doc->appendchild($doc->createelement('inits')); $root->appendchild($doc->createelement('version',$patchnum)); $root->appendchild($doc->createelement('source',$sourcetext)); $root->appendchild($doc->createelement('lowid',$lowid)); $root->appendchild($doc->createelement('highid',$highid)); $root->appendchild($doc->createelement('ql',$ql)); for($i=11;$i>=0;$i--) { $node = $root->appendchild($doc->createelement('inits')); $node->setattribute("slider","def>".str_repeat("=",$i)."][".str_repeat("=",11-$i)."<agg"); $node->setattribute("percent",floor($i/11*100)); $node->setattribute("init",$i>3 ? 430+(11-$i)*100 : 1290+(3-$i)*300); }
php xml domdocument
Comments
Post a Comment