如何使用SimpleXmlElement将输入元素写入xml文件?

时间:2012-10-04 19:15:34

标签: php

这就是我现在所使用的:

//variable assignment that create strings from text input element
$xmlUsernameStr = $xmlDetail->userString();

//this is the function used above
public function userString(){
    return $user;//this is the variable that holds username on input element
}

//assignments in php script 
$sxml = new SimpleXmlElement(file_get_contents('details.xml'));
$xmlDetailTag = new SimpleXMLElement('<detail></detail>');  

//details.xml contains:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<details>
<detail></detail>
</details>

//create another child 'user' within parent 'detail' 
$aUserTag = $sxml->detail->addChild('user');

那么,如果我所拥有的是正确的实际写入xml的内容?

1 个答案:

答案 0 :(得分:0)

$xml = simplexml_load_file('details.xml');
$xml->addChild('details')->addChild('detail', $xmlUsernameStr );
file_put_contents('details.xml', $xml->asXML());