XML生成方法
2017-03-20 11:28:09
通过构造数组。再执行生成函数即可
$data=array(
'config'=>array(
'list'=>array(
'tollgate_id'=>'tollgate_id',
'tollgate_name'=>'tollgate_name',
'tollgate_life'=>'tollgate_life',
'tollgate_wave'=>'tollgate_wave',
'tollgate_swf'=>'tollgate_swf',
)
)
);
$this->CreateXml($data,'../swf/config/','tollgate.xml');
public function CreateXml($data,$url,$filename,$permission='')
{
if($permission)$permission = 0777;
//生成XML
$DOM = new DOMdocument('1.0', 'utf-8');
foreach ($data as $key=>$vd)
{
//创建主元素
$main_list = $DOM->createElement($key);
$DOM->appendChild($main_list);
foreach ($vd as $k=>$value)
{
//创建config下的list元素
$list = $DOM->createElement($k);
$main_list->appendChild($list);
foreach ($value as $vkey=>$vv)
{
$element = $DOM->createElement($vkey);
$list->appendChild($element);
$element_value = $DOM->createTextNode($vv);
$element->appendChild($element_value);
}
}
}
$DOM->saveXML();//保存生成的XML内容
$xml_filename = $url.$filename;//文件名 ../swf/config/wave.xml
$DOM->save($xml_filename);//写入文件
chmod($xml_filename, $permission);//设置权限 0777(权限全开) 0755(读取,执行) 0644 0666(读取,写入)
}
获取XML
//xml 处理
function BehillXml($filename,$tag)
{
$xml = new DOMDocument();
$xml->load($filename);
foreach($xml->getElementsByTagName($tag) as $vlist)
{
$value[] = $vlist->firstChild->nodeValue;
}
return $value;
}
发表评论: