Excavating Old Blog Posts

| Tuesday, June 23, 2009

I actually really miss my old blog.nuggit.nu. It was poorly designed and poorly coded, yet it's utter simplicity and homeyness was irresistible to me, like a brand new cheap notepad just begging to be written in, absolutely anything. I love when I get that feeling from paper or software. It really nourishes inspiration. So I posted there pretty often. It didn't get too many visitors, but my faithful sister read it once in a while, :) so at least that pushed me to have just enough of a quality standard when posting. I loved it.

That ended when I moved to college. The server was taken down to be set in the dorm room (never to be used again, really, except to get info off of). At least I got the blog database information off before taking out the hard drive to try to house it in a different computer (which didn't work, so that one is now sitting dead on my desk). The problem was, I exported it in XML format. I figured XML was compatible with anything and everything. Right? The important thing was, last I saw, phpmyadmin could take XML to import data.

Ahahaha.

Preparing to upload the XML file to the myphpadmin that nuggit.nu currently uses, I D:'d at SQL being the only file format option to import with. I even booted into Linux to try to import with myphpadmin on there, but it was the same. What happened?! I clearly remember back in the day, having to use XML to import to phpmyadmin... as I Googled this problem, it gradually settled that I probably remembered the complete opposite thing. I probably tried XML first because it was familiar, then had to use SQL. Everywhere I looked recommended SQL for backups.

What a pain in the ass. There were many others that made the same mistake as I, but nobody really had a solution. Of course there were several possible ways to go with it. I could learn MySQL's command line interface, and use ExtractData() in some way. If I knew "a bit of PHP" someone said I could "easily" write a script to read data from the XML file. Yeah, right. I saw that more than a few times, yet people would never give a starting pointer to work from. It is not easy from where I'm standing. I hate when they're so vague.

...it was not til I Googled it while writing this blog post that I found the SimpleXML extension for PHP that does seem to make it quite simple to get data.

I'm going to work on this. I will get my old site back up, at least partially.

3 comments:

Thomas said...

Its been a while since I did a conversion from xml to mysql with php.

http://xkcd.com/327/

ND said...

Haha, that's one of my favorites.

Did you use SimpleXML when you did that?
I'm about to post another blog with some amateur code. I'm almost scared of what you'd think, now.

Thomas said...

I used the following, I stole it from somewhere. I've become better about putting a comment with where I got stuff after I wrote this.

And don't worry, where I'm working I feel like the idiot programmer.



class XMLParser
{
var $parser;
var $filePath;
var $document;
var $currTag;
var $tagStack;

function XMLParser($path)
{
$this->parser = xml_parser_create();
$this->filePath = $path;
$this->document = array();
$this->currTag =& $this->document;
$this->tagStack = array();
}

function parse()
{
xml_set_object($this->parser, $this);
xml_set_character_data_handler($this->parser, 'dataHandler');
xml_set_element_handler($this->parser, 'startHandler', 'endHandler');

if(!($fp = fopen($this->filePath, "r")))
{
die("Cannot open XML data file: $this->filePath");
return false;
}

while($data = fread($fp, 4096))
{
if(!xml_parse($this->parser, $data, feof($fp)))
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->parser)),
xml_get_current_line_number($this->parser)));
}
}

fclose($fp);
xml_parser_free($this->parser);

return true;
}

function startHandler($parser, $name, $attribs)
{
if(!isset($this->currTag[$name]))
$this->currTag[$name] = array();

$newTag = array();
if(!empty($attribs))
$newTag['attr'] = $attribs;
array_push($this->currTag[$name], $newTag);

$t =& $this->currTag[$name];
$this->currTag =& $t[count($t)-1];
array_push($this->tagStack, $name);
}

function dataHandler($parser, $data)
{
$data = trim($data);

if(!empty($data))
{
if(isset($this->currTag['data']))
$this->currTag['data'] .= $data;
else
$this->currTag['data'] = $data;
}
}

function endHandler($parser, $name)
{
$this->currTag =& $this->document;
array_pop($this->tagStack);

for($i = 0; $i < count($this->tagStack); $i++)
{
$t =& $this->currTag[$this->tagStack[$i]];
$this->currTag =& $t[count($t)-1];
}
}
}