Skip to content

Instantly share code, notes, and snippets.

@ozero
Created February 24, 2011 06:06
Show Gist options
  • Save ozero/841839 to your computer and use it in GitHub Desktop.
Save ozero/841839 to your computer and use it in GitHub Desktop.
Load Eijirou-XML into PostgreSQL
<?php
require_once 'Zend/Db.php';
require_once 'Zend/Config/Ini.php';
//connect db
$db=dbfactory();
// Parsing a large document with XMLReader with Expand - DOM/DOMXpath
$reader = new XMLReader();
$reader->open("EIJI-127.xml");
$rec=array();
$rec_key="";
$loop=0;
while ($reader->read()) {
if($reader->isEmptyElement){continue;}
switch ($reader->nodeType) {
case (XMLREADER::ELEMENT):
if($reader->depth < 2){
post2db($db, $rec);
$rec = array();
if($loop%500 == 0){print "{$loop}\n";}
$loop++;
}
$rec_key = $reader->localName;
break;
case (XMLREADER::TEXT):
$rec[$rec_key] = $reader->value;
break;
}
}
function post2db(&$db, $arg){
if($arg['word']==""){return;}
$sql="insert into kwd(keyword,word,trans,exp,level,memory,modify,pron,filelink)
values(:keyword,:word,:trans,:exp,:level,:memory,:modify,:pron,:filelink);";
$valsrc=array('keyword'=>'','word'=>'','trans'=>'','exp'=>'','level'=>'',
'memory'=>'','modify'=>'','pron'=>'','filelink'=>'');
$arg['keyword'] = substr($arg['word'],0,250);
$bindval=array_merge($valsrc,(array)$arg);
$rs = $db->query($sql,$bindval);
return;
}
function dbfactory(){
$config = new Zend_Config_Ini('./db.ini', 'default');
$db = Zend_Db::factory($config->db);
$db->setFetchMode(Zend_Db::FETCH_OBJ);
return $db;
}
/*
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;
SET search_path = public, pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: kwd; Type: TABLE; Schema: public; Owner: user; Tablespace:
--
CREATE TABLE kwd (
id bigint DEFAULT nextval('seq_kwd_id'::regclass) NOT NULL,
keyword character varying(255),
word text,
trans text,
exp character varying(255),
level character varying(255),
memory character varying(255),
modify character varying(255),
pron character varying(255),
filelink character varying(255)
);
ALTER TABLE public.kwd OWNER TO "user";
--
-- Name: kwd_pkey; Type: CONSTRAINT; Schema: public; Owner: user; Tablespace:
--
ALTER TABLE ONLY kwd
ADD CONSTRAINT kwd_pkey PRIMARY KEY (id);
--
-- PostgreSQL database dump complete
--
*/
/*
db.ini
[default]
db.adapter = Pdo_Pgsql
db.params.host = localhost
db.params.username = user
db.params.password = passwd
db.params.dbname = sampledb
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment