操作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 ;
?>
说明:显示<baby><class><toy>(第二个)下<name>的文本!
②:显示某一结点下所有子结点的数据:
<?php
$xml = simplexml_load_file("test.xml");
foreach($xml->baby->class->toy[1]->children() as $child){
echo $child->getName() . ": " . $child . "<br />";
}?>
说明:显示<baby><class><toy>(第二个)所有子结点的名称和数据!
修改
<?php
$xml = simplexml_load_file("test.xml");
foreach($xml->baby->class->toy[1]->children() as $child){
echo $child->getName() . ": " . $child . "<br />";
}?>
说明:修改<baby><class><toy>(第二个)下<name>的文本为“小咪咪裙子”!