php - simple xml add attribute -
how can set attributes when add new elements in xml php. php code this:
<?php $xml = simplexml_load_file ( 'log.xml' ); $movies = $xml->addchild("time"); // add attribut `value` here in tag time $user = $movies->addchild("user", ""); // add attribut `id` here in tag user $action = $user->addchild("action",""); // add attribut `value` here in tag action $action->addchild("table","customers"); $action->addchild("table_id","1"); echo $xml->savexml( 'log.xml' ); ?>
and want output this:
// log.xml <?xml version="1.0" encoding="utf-8"?> <log> <time value="2013-01-10 12:20:01"> <user id="1"> <action value="delete"> <table>customer</table> <table_id>1</table_id> </action> <action value="insert"> <table>customer</table> <data> <nama>budi</nama> </data> </action> <action value="update"> <table>customer</table> <table_id>1</table_id> <old_data> <nama>andi</nama> </old_data> <new_data> <nama>budi</nama> </new_data> </action> </user> </time> </log>
please me..i new xml
use simplexmlelement::addattribute — adds attribute simplexml element
edit: use case -
$action = $user->addchild("action",""); // add attribut `value` here in tag action $action->addattribute('value','update'); // add $action->addchild("table","customers"); $action->addchild("table_id","1");
best example:
http://php.net/manual/en/simplexmlelement.addattribute.php
<?php include 'example.php'; $sxe = new simplexmlelement($xmlstr); $sxe->addattribute('type', 'documentary'); $movie = $sxe->addchild('movie'); $movie->addchild('title', 'php2: more parser stories'); $movie->addchild('plot', 'this people make work.'); $characters = $movie->addchild('characters'); $character = $characters->addchild('character'); $character->addchild('name', 'mr. parser'); $character->addchild('actor', 'john doe'); $rating = $movie->addchild('rating', '5'); $rating->addattribute('type', 'stars'); echo $sxe->asxml(); ?>
credits first example in php.net ref page...
Comments
Post a Comment