<!DOCTYPE html> <?php $data_array = array(array('title' => 'title1', 'content' => 'content1', 'pubdate' => '2009-10-11'), array('title' => 'title2', 'content' => 'content2', 'pubdate' => '2009-11-11')); // 属性数组 $attribute_array = array('title' => array('size' => 1)); // 创建一个XML文档并设置XML版本和编码。。 $dom = new DomDocument('1.0', 'utf-8'); // 创建根节点 $article = $dom->createElement('article'); $dom->appendchild($article); foreach ($data_array as $data) { $item = $dom->createElement('item'); $article->appendchild($item); create_item($dom, $item, $data, $attribute_array); } echo $dom->saveXML(); function create_item($dom, $item, $data, $attribute) { if (is_array($data)) { foreach ($data as $key => $val) { // 创建元素 ${$key} = $dom->createElement($key); $item->appendchild(${$key}); // 创建元素值 $text = $dom->createTextNode($val); ${$key}->appendchild($text); if (isset($attribute[$key])) { // 如果此字段存在相关属性需要设置 foreach ($attribute[$key] as $akey => $row) { // 创建属性节点 ${$akey} = $dom->createAttribute($akey);
include_once 'QPage.php'; include_once 'QData.php'; include_once 'QUrl.php'; include_once 'config.php'; $date = date('Ymd'); $data = array(); $rootDir = 'result'; foreach ($QData as $key => &$keyWord) { $jsonDir = "{$rootDir}/{$date}/{$key}.json"; if (file_exists($jsonDir)) { $code = file_get_contents($jsonDir); $r = json_decode($code); $result = explode('|', $r->data->index->{$key}); $data[$key]['id'] = $key; $data[$key]['name'] = $keyWord; for ($i = 0; $i < 5; $i++) { $rowName = 'day' . ($i + 1); $data[$key][$rowName] = array_pop($result); } $data[$key]['url'] = QPage::getURL($key, $QUrl[QPURL]); } } $dom = new DomDocument('1.0', 'utf-8'); $list = $dom->createElement('list'); $dom->appendchild($list); foreach ($data as $value) { QPage::createItem($dom, $list, array('item' => $value)); } $xmlDir = "{$rootDir}/{$date}.xml"; $dom->save($xmlDir); echo "Finish!\n";