操作xml,即是:读取和修改xml文档的数据!
读取:
①:读取某一固定结点的数据:
if (file_exists('test.xml')) {
$xml = simplexml_load_file('test.xml');
} else {
exit('Failed to open test.xml.');
}
- <?php
- $xml = simplexml_load_file("data.xml");
- echo $xml->baby->class->toy[1]->name ;
- ?>
②:显示某一结点下所有子结点的数据:
- <?php
- $xml = simplexml_load_file("test.xml");
- foreach($xml->baby->class->toy[1]->children() as $child){
- echo $child->getName() . ": " . $child . "<br />";
- }?>
修改
- <?php
- $xml = simplexml_load_file("test.xml");
- foreach($xml->baby->class->toy[1]->children() as $child){
- echo $child->getName() . ": " . $child . "<br />";
- }?>