addChild
向SimpleXML 元素添加子元素。
addChild()
函數用於向SimpleXML 元素添加子元素。
向<body> 元素添加子元素,並添加一個新的<footer> 元素:
<?php $note = <<< XML <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Do not forget the meeting!</body> </note> XML ; $xml = new SimpleXMLElement ( $note ) ; // 向body 元素添加子元素 $xml -> body -> addChild ( "date" , "2016-01-01" ) ; // 在note 內部的最後一個元素後添加子元素 $footer = $xml -> addChild ( "footer" , "Some footer text" ) ; echo $xml -> asXML ( ) ; ?>
運行實例
SimpleXMLElement :: addChild ( name , value , ns )
參數 | 描述 |
---|---|
name | 必需。指定要添加的子元素的名稱。 |
value | 可選。指定子元素的值。 |
ns | 可選。指定子元素的命名空間。 |