欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

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

程序员文章站 2022-05-12 09:15:45
...
适用平台: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]['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!");
}