Rumah >php教程 >PHP源码 >在Drupal导入外部xml成节点数据

在Drupal导入外部xml成节点数据

PHP中文网
PHP中文网asal
2016-05-25 17:14:081067semak imbas

适用平台:drupal6.X

项目背景:如果你想把外部的xml文件数据导成Drupal的节点数据,那么这段小程序非常适用,希望能帮到你,当时写的时候费了我好长时间哦~

// Implements hook_menu
function importxml_menu() {
 $items = array();
 $items['importxml'] = array(
 'title' => t('Import Xml'),
 'description' => t('Import Xml'),
 'access callback' => 'config_access',
 'page callback' => 'drupal_get_form',
 'page arguments' => array('import_form'),
 'type' => MENU_NORMAL_ITEM,
 'weight' => -10,
);
 return $items;
}
function config_access() {
 return (($GLOBALS['user']->uid == 1));
}

/**
 * Builds the import form.
*/
function import_form($form_state) {

 $form['data_source'] = array(
 '#type' => 'fieldset',
 '#attributes' => array('id' => 'data_source'),
);


 $form['data_source']['upload_file'] = array(
 '#type' => 'file',
 '#title' => t('File to import'),
 '#description' => t('Click"Browse..."to select a local document to upload.'), 
);



 $form['submit'] = array(
 '#type' => 'submit',
 '#value' => t('Import'),
);

 $form['#attributes'] = array('enctype' => 'multipart/form-data');

 return $form;
}

function import_form_submit($form, &$form_state) {

 $validators = array('file_validate_extensions' => array('upload_file'),);
 if ($file = file_save_upload('upload_file', $validators)) {
 $fd = fopen($file->filepath,"rb");
 if (!$fd) {
 form_set_error('upload_file', t('Import failed: file %filename cannot be read.', array('%filename' => $file->filename)));
}
 else {
 $info = fstat($fd);
 $len = $info["size"];
 $text = fread($fd, $len);
fclose($fd);
 drupal_set_message(t('Loaded file %filename. Now processing it.', array('%filename' => $file->filename)));
 $form_state['values']['file'] = $file;

 import_xml_invoke_import($text, $form_state['values']);
}
}
 else {
 form_set_error('upload_file', t('Import failed: file was not uploaded.'));
}
}




 function parseMol($mvalues) {
 for ($i=0; $i < count($mvalues); $i++)
 $mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
 return new ImportXml($mol);
}


class ImportXml {

 var $shuming; 
 var $congshuming; 
 var $fushucongshuming; 
 var $zhuzuozhe;
 var $chubanzhe;
 var $isbn;

 function ImportXml ($aa) {
 foreach ($aa as $k=>$v)
 $this->$k = $aa[$k];
}
}



/**
 * Do the actual importing from the given string, pased on the parameters passed
 * from the form.
*
 * @param $text
*/
function import_xml_invoke_import(&$text) {

 // parse the data:
 $xml_parser = drupal_xml_parser_create($text);
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct($xml_parser,$text,$values,$tags);
xml_parser_free($xml_parser);

// now begin fetch the value

 foreach ($tags as $key=>$val) {

 if ($key =="tushu") {

 $molranges = $val;
 for ($i=0; $i < count($molranges); $i+=2) {
 $offset = $molranges[$i] + 1;
 $len = $molranges[$i + 1] - $offset;
 $tdb[] = parseMol(array_slice($values, $offset, $len));
}
 } else {
continue;
}
}

foreach($tdb as $value){

 $node = array();
 $node = new stdClass;
 $node->type ="product";
 $node->status = 1;
 $node->uid = 1;
 $node->title = $value->shuming;
 $node->body = $value->tiyao;
 $node->field_tushu_congshuming[0][&#39;value&#39;] = $value->congshuming;
 $node->field_tushu_fushucongshuming[0][&#39;value&#39;] = $value->fushucongshuming;
 $node->field_tushu_zhuzuozhe[0][&#39;value&#39;] = $value->zhuzuozhe;
 $node->field_tushu_chubanzhe[0][&#39;value&#39;] = $value->chubanzhe;
 $node->field_tushu_isbn[0][&#39;value&#39;] = $value->isbn;
node_save($node);
}

drupal_set_message("Import Successful!");
}
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:UTF-8编码怎么去掉BOM头Artikel seterusnya:PHP检验ISBN码的函数