#!/usr/bin/perl -w # ISHIHARA Yoshinori 2005.6.14 # $Id use strict; use CGI; use HTML::Template; use XML::LibXML; use Encode qw( encode ); use Tie::IxHash; ## 引数取得 my $q = CGI->new; my $xml = $ARGV[0] || $q->param('xml'); my $tmpl = $ARGV[1] || $q->param('tmpl') || "edit_chronicle.tmpl"; my $html = HTML::Template->new(filename => $tmpl); ## パーズ my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($xml); my $root = $doc->getDocumentElement; ## トラバース my $title; my $area; my $text; my @units; proc_node($root, \@units); ## セット $html->param(TITLE => $title); $html->param(AREA => $area); $html->param(TEXT => $text); $html->param(UNIT => \@units); ## 出力 print "Content-type: text/html\n\n"; print $html->output; exit; sub proc_node { my ($node, $parent) = @_; return unless ($node->nodeType eq XML_ELEMENT_NODE()); our $unitid; unless ($unitid) { $title = get_cdata($node) if ($node->nodeName eq 'title'); $area = get_cdata($node) if ($node->nodeName eq 'area'); $text = get_cdata($node) if ($node->nodeName eq 'text'); } if ($node->nodeName eq 'unit') { my (%hash, @array); push @{$parent}, \%hash; $hash{POINT} = \@array; $unitid++; $hash{UNITID} = $unitid; $hash{DATE} = $node->getAttribute('date'); $parent = \@array; } if ($node->nodeName eq 'point') { my %hash; $hash{UNITID} = $unitid; push @{$parent}, \%hash; $parent = \%hash; } if ($node->nodeName eq 'name' && $node->parentNode()->nodeName eq 'point' ) { my $string = get_cdata($node); $parent->{NAME} = $string; } if ($node->nodeName eq 'time' && $node->parentNode()->nodeName eq 'point' ) { my $string = get_cdata($node); $parent->{TIME} = $string; } if ($node->nodeName eq 'text' && $node->parentNode()->nodeName eq 'point' ) { my $string = get_cdata($node); $parent->{TEXT} = $string; } foreach my $child ($node->getChildnodes) { my $name = $child->nodeName; proc_node($child, $parent) } } sub get_cdata { my $node = shift; my $string; foreach my $child ($node->getChildnodes) { $string .= $child->toString(); } return $string; }