热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

在Drupal导入外部xml成节点数据-PHP源码

在Drupal导入外部xml成节点数据
适用平台: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 $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 type ="product";
 $node->status = 1;
 $node->uid = 1;
 $node->title = $value->shuming;
 $node->body = $value->tiyao;
 $node->field_tushu_congshuming[0]['value'] = $value->congshuming;
 $node->field_tushu_fushucongshuming[0]['value'] = $value->fushucongshuming;
 $node->field_tushu_zhuzuozhe[0]['value'] = $value->zhuzuozhe;
 $node->field_tushu_chubanzhe[0]['value'] = $value->chubanzhe;
 $node->field_tushu_isbn[0]['value'] = $value->isbn;
node_save($node);
}

drupal_set_message("Import Successful!");
}

推荐阅读
author-avatar
夜月丶凉如诗
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有