Friday, March 21, 2008

Freemind map <--> lisp (perl, python) list convertion (III)



This post presents way to push freemind map to tree list (array) structure in perl.

My first idea was to use XSLT tranformers to get result.

However, it is appears that XSLT is intended for stylesheet conversion, and W3c alarmed, that it is not for transforming one XML of general form. And my first investigations of XPath showed that it is not trivial to process branches one after one in order.

It is easily to get subtree of already defined structure with XSLT, however it seems that it is not intended for processing of tree's with undefined structure.

Thats way, tried to implement it in perl,
with usage of the XML::LibXML.

I have succeeded to install perl XML modules on Gutsy only after switching to English Locale:

export LANG=en_US.UTF-8
sudo apt-get install libxml-libxml-perl


The program in listing prints nodes as plain text:


sub main {
# do processing
my $parser = XML::LibXML->new;

my $doc = $parser->parse_file('map.mm')
or die "can't parse file: $@";

print "doc parameters: ref:" . ref($doc);
## prints all map
#print $doc->serialize();

my @nodelist = $doc->getElementsByTagName("node");

foreach my $node (@nodelist)
{
my @temps=();
foreach my $attr ($node->attributes())
{
print ":". $attr->value() .":";
};
print "[" . $node->getAttribute("TEXT") . "]\n";
}
}

main();


Output is:


:1203720731508::Freemind_Link_208208947::1203720850994::map:[map]
:1203720752265::_::1203720864185::right::check:[check]
:1203720869928::Freemind_Link_1814940840::1203720883570::check1:[check1]
:1203720883879::Freemind_Link_1671485576::1203720886018::check2:[check2]
:1203720886554::Freemind_Link_1788632798::1203720888590::check3:[check3]
:1204847625761::Freemind_Link_1338835501::1204847634916::ch3ch1:[ch3ch1]
:1204833937558::Freemind_Link_1918171523::1204833942841::left::node:[node]
:1204833944619::Freemind_Link_201749731::1204833962514::subnode1:[subnode1]
:1204833951408::Freemind_Link_749106103::1204833962515::subnode2:[subnode2]
:1204847691307::Freemind_Link_659319167::1204847697178::right::trnode:[trnode]

If you want
to process map recursively, to package or present in another tree structure, following function have to be used:

@nodes = $node->childNodes

No comments: