php - Adding Nodes to Existing XML -
the problem having root xml being produced every time writes xml. main issue setting child , defining root. of Łza understand root xml node ignored. setup , create child , add content, , example of correct format is.
$xml = simplexml_load_file('filename.xml'); // load xml file need add if statment create if not exist $result = $xml->addchild('result'); // ignore root node , add child results $result->addchild('time', gmdate('d-m-y -h:i:s')); // rest of below adds child result , outputs results $result->addchild('channel', $site); $result->addchild('type', '**'); $result->addchild('process', $status); $result->addchild('sku', $code->sku); $result->addchild('item', $item); $result->addchild('status', '$feedback'); $result->addchild('errorid', '$error'); $result->addchild('message', '$message'); $xml->asxml('filename.xml'); //write file // of above code using variables part of script the output be
<root> <result> <time>fri-may-2013 -09:15:22</time> <channel>20</channel> <type>**</type> <process>update</process> <sku>98746524765</sku> <item/> <status>problem</status> <errorid>999-error</errorid> <message>unknown file format support</message> </result> <result> <time>fri-may-2013 -09:15:22</time> <channel>20</channel> <type>**</type> <process>update</process> <sku>5412254785</sku> <item/> <status>problem</status> <errorid>123-error</errorid> <message>invalid item</message> </result> </root> thanks
try use simplexmlelement library instead hardcoded xml creation. maybe more complicate use @ begining, more safe (i mean avoid possible errors in xml structure when hardcode xml) , easy use when start use it. , easy add/remove nodes, childnodes.
this example code:
$xml = new simplexmlelement('<xml/>'); $data = $xml->addchild('data'); $result = $data->addchild('result'); $result->addchild('time', gmdate('d-m-y -h:i:s')); $result->addchild('channel', $siteid); // ... , same way create xml nodes. // if want add next <result> node witch elements repeat code, (or put in loop if want more <result> elements): $result = $data->addchild('result'); $result->addchild('time', gmdate('d-m-y -h:i:s')); $result->addchild('channel', $siteid); // , after create nodes save file: $xml->asxml('dherror.xml'); above code create xml:
<xml> <data> <result> <time>fri-may-2013 -12:14:39</time> <channel>data</channel> </result> <result> <time>fri-may-2013 -12:14:39</time> <channel>data</channel> </result> </data> </xml> thats it. if need load , process xml easy:
to load file use:
$xml2 = simplexml_load_file('dherror.xml'); // add new node <result>: $resultnext = $xml2->data->addchild('result'); $resultnext->addchild('time', gmdate('d-m-y -h:i:s')); $resultnext->addchild('channel', $siteid); //and save file $xml2->asxml('dherror.xml'); this create xml:
<?xml version="1.0" ?> <xml> <data> <result> <time>fri-may-2013 -12:27:24</time> <channel>data</channel> </result> <result> <time>fri-may-2013 -12:27:24</time> <channel>data</channel> </result> <result> <time>fri-may-2013 -12:27:24</time> <channel>data</channel> </result> </data> </xml>
Comments
Post a Comment