批量导入XML数据到drupal系统的方法
发布时间:2014-11-10 10:26:33 作者:佚名 我要评论
这篇文章主要为大家介绍了批量导入XML数据到drupal系统的方法,主要通过一个XML文件导入类来实现这一功能,是非常实用的技巧,需要的朋友可以参考下
本文实例讲述了批量导入XML数据到drupal系统的方法。分享给大家供大家参考。具体实现方法如下:
如果你想把其它网站的数据批量导入到drupal系统中,可以采用本文所述代码。前提条件是,你要把原来网站的数据生成XML格式!
生成XML的工具有很多,有个叫 xml.class.php的类,可以试用一下,你也可以自己写PHP代码来实现。
生成XML文件后,通过这个模块,直接上传,就可以把它导入到drupal系统了。
该模块还可以对你导入的数据进行自动分类(Taxonomy整合)。
以下为该模块的部分精华源代码,感兴趣的朋友可以测试运行一下本实例。主要功能代码如下:
复制代码
代码如下:<?php
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 $tushushangpin;
var $shangpindaima;
var $shuming;
var $congshuming;
var $fushucongshuming;
var $zhuzuozhe;
var $chubanzhe;
var $benbanbanci;
var $yinci;
var $dingjia;
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 == "tushushangpin") {
$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->neirongtiyao;
$node->field_product_shangpindaima[0]['value'] = $value->shangpindaima;
$node->field_product_shuming[0]['value'] = $value->shuming; // use ubercart
$node->field_product_congshuming[0]['value'] = $value->congshuming;
$node->field_product_fushucongshuming[0]['value'] = $value->fushucongshuming;
$node->field_product_zhuzuozhe[0]['value'] = $value->zhuzuozhe;
$node->field_product_chubanzhe[0]['value'] = $value->chubanzhe;
$node->field_product_benbanbanci[0]['value'] = $value->benbanbanci;
$node->field_product_yinci[0]['value'] = $value->yinci;
$node->field_product_dingjia[0]['value'] = $value->dingjia;
// if $value->tongjifenlei is not null then :
if($value->tongjifenlei){
$tj_vid = 1;
if($tid = (int) db_result(db_query('SELECT tid FROM {term_data} WHERE name = "%s" AND vid = %d', $value->tongjifenlei,$tj_vid))){
$tj_tid = $tid;
}else{
// vocabulary ID is hard coded for this example
$autoterm = array(
'name' => $value->tongjifenlei, // or whatever you want the auto-term to be named
'parent' => 0,
'vid' => $tj_vid,
);
taxonomy_save_term($autoterm);
$tj_tid = (int) db_result(db_query('SELECT MAX(tid) FROM {term_data} WHERE vid = %d', $vid));
}
$node->taxonomy[$tj_vid][$tj_tid] = $tj_tid;
}
node_save($node);
}
drupal_set_message("Import Successful!");
}
?>
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 $tushushangpin;
var $shangpindaima;
var $shuming;
var $congshuming;
var $fushucongshuming;
var $zhuzuozhe;
var $chubanzhe;
var $benbanbanci;
var $yinci;
var $dingjia;
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 == "tushushangpin") {
$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->neirongtiyao;
$node->field_product_shangpindaima[0]['value'] = $value->shangpindaima;
$node->field_product_shuming[0]['value'] = $value->shuming; // use ubercart
$node->field_product_congshuming[0]['value'] = $value->congshuming;
$node->field_product_fushucongshuming[0]['value'] = $value->fushucongshuming;
$node->field_product_zhuzuozhe[0]['value'] = $value->zhuzuozhe;
$node->field_product_chubanzhe[0]['value'] = $value->chubanzhe;
$node->field_product_benbanbanci[0]['value'] = $value->benbanbanci;
$node->field_product_yinci[0]['value'] = $value->yinci;
$node->field_product_dingjia[0]['value'] = $value->dingjia;
// if $value->tongjifenlei is not null then :
if($value->tongjifenlei){
$tj_vid = 1;
if($tid = (int) db_result(db_query('SELECT tid FROM {term_data} WHERE name = "%s" AND vid = %d', $value->tongjifenlei,$tj_vid))){
$tj_tid = $tid;
}else{
// vocabulary ID is hard coded for this example
$autoterm = array(
'name' => $value->tongjifenlei, // or whatever you want the auto-term to be named
'parent' => 0,
'vid' => $tj_vid,
);
taxonomy_save_term($autoterm);
$tj_tid = (int) db_result(db_query('SELECT MAX(tid) FROM {term_data} WHERE vid = %d', $vid));
}
$node->taxonomy[$tj_vid][$tj_tid] = $tj_tid;
}
node_save($node);
}
drupal_set_message("Import Successful!");
}
?>
希望本文所述对大家的drupal建站有所帮助。
相关文章
- 对页面和静态资源的启用缓存和Gzip压缩传输.2010-01-24
- drupal模块开发分析,方便想要drupal模块开发的朋友2012-12-06
drupal 自定义表单调用autocomplete主标签实现代码
drupal 自定义表单调用autocomplete主标签实现代码,需要的朋友可以参考下2012-12-06- 这篇文章主要为大家介绍了drupal实现在node节点的评论下面添加内容的方法,涉及相关函数的修改与hook函数的使用,具有一定的借鉴价值,需要的朋友可以参考下2014-11-04
- 这篇文章主要为大家介绍了Drupal实现不同节点类型使用不同模板的方法,是针对Drupal主题机制的灵活应用,需要的朋友可以参考下2014-11-05
- 这篇文章主要为大家介绍了定制Drupal首页的方法,以实例形式讲述了几种常见的实现方法,非常实用,需要的朋友可以参考下2014-11-05
- 这篇文章主要为大家介绍了Drupal第三方模块,较为详细的罗列了drupal常用的第三方模块及其对应的下载地址,对于drupal建站来说具有很好的参考借鉴价值,需要的朋友可以参考下2014-11-06
- 这篇文章主要为大家介绍了Drupal采用钩子系统删除用户的实现方法,对钩子系统的运行机制做了较为详细的叙述,具有很好的参考借鉴价值,需要的朋友可以参考下2014-11-06
- 这篇文章主要为大家介绍了drupal的url传值和表单传值,以实例形式较为详细的分析了drupal的URL传值与表单传值的具体实现方法,是采用drupal建站中非常实用的技巧,需要的朋友2014-11-07
- 这篇文章主要为大家介绍了drupal导入图片的实现方法,是非常实用的技巧,需要的朋友可以参考下2014-11-03
最新评论