php상에서 동적으로 xml파일 생성하기

Posted by Albert 4151Day 15Hour 1Min 32Sec ago [2013-12-09]

<?php
'使用dom生成xml,注意生成的xml中会没有空格。
$dom=new DOMDocument('1.0','utf-8');
$path="test.xml"; // $path 为xml文件的存储路径。

$module=$dom->createElement('newmodule');// root node
$dom->appendChild($module);

$year=$dom->createElement('year'); // add attribute node
$name=$dom->createAttribute('name');
$name->nodeValue="最新动态";
$year->setAttributeNode($name);
$module->appendChild($year);

$news=$dom->createElement('news');
$year->appendChild($news);

$date=$dom->createElement('date');
$date_value=$dom->createTextNode('01-24');
$date->appendChild($date_value);
$news->appendChild($date);

$title=$dom->createElement('title');
$title_value=$dom->createTextNode('<![CDATA[最新动态]]>');
$title->appendChild($title_value);
$news->appendChild($title);

$info=$dom->createElement('info');
$info_value=$dom->createTextNode('<![CDATA[ ㅌㅌㅌㅌㅌ。
]]>');
$info->appendChild($info_value);
$news->appendChild($info);

echo $dom->saveXML();
$dom->save($path);
?>


만약 Fatal error: Class 'DOMDocument' 에러가 날때에는 
서버에 반드시 
' yum install php-xml
' service httpd restart
php-xml을 설치 및 아파치 재시작시켜주어야한다.



LIST

Copyright © 2014 visionboy.me All Right Reserved.