add different children to an element in a xml file using php -
i want add children element on xml file :
<test> <parameter type="double" name="phone_number" /> <parameter type="string" name="name" /> <parameter type="string" name="e-mail" /> ... </test>
i've tried somthing :
$input = simplexml_load_file('new.xml'); $input->test=""; $input->test->addchild("parameter"); $input->test->parameter->addattribute("type", "double"); $input->test->parameter->addattribute("name", "phone_number"); $input->test->addchild("parameter"); $input->test->parameter->addattribute("type", "string"); $input->test->parameter->addattribute("name", "name"); ...
but error message :
warning: simplexmlelement::addattribute() [simplexmlelement.addattribute]: attribute exists
how solve problem?
1st, cant access xml root element wrap xml examle '<xml>'.$yourxml.'</xml>
2nd simplexml doesn't understand parametr tag access. save point added child , modify it
$str = '<xml><test> <parameter type="double" name="phone_number" /> <parameter type="string" name="name" /> <parameter type="string" name="e-mail" /> </test></xml>'; $input = simplexml_load_string($str); $input->test=""; $child = $input->test->addchild("parameter"); $child->addattribute("type", "double"); $child->addattribute("name", "phone_number"); $child = $input->test->addchild("parameter"); $child->addattribute("type", "string"); $child->addattribute("name", "name"); echo $input->savexml();
Comments
Post a Comment