in reply to VB .frm File to XML file
You could use XML::Writer, but in this case it looks simple enough (famous last words!) so basic print statements probably work just fine. You could also generate SAX events from the file and then let a SAX writer take care of it of course.
Anyway, my attempt is below, it stores everything in the form.
Problems you might encounter:
#!/bin/perl -w use strict; my @context; my $FORM_TAG = "form"; my $SUB_TAG = "sub"; my $INDENT = " " x 2; while( <DATA>) { if( m{^\s*Begin\s+(\S+)\s+(\S+)\s*$}) { # Begin tag name my( $tag, $name)= ($1, escape_xml($2)); print $INDENT x @context; print qq{<$tag name="$name">\n}; push @context, $tag; } elsif( m{^\s*(\S+)\s*=\s*(.*)$}) { # key = value my( $tag, $value)= ($1, $2); print $INDENT x @context; $value= escape_xml( strip_quotes( $value)); print qq{<$tag>$value</$tag>\n}; } elsif( m{^\s*End\s*$}) { # End my $tag= pop @context; print $INDENT x @context; print qq{</$tag>\n}; } elsif( m{^Attribute\s*(\S*)\s*=\s*(.*)$}) { # Attribute att = val # Note: those could be stored as attribute to the main tag # but this would increase the code complexity as it # would need to buffer most of the data my( $attribute, $value)= ($1, $2); print $INDENT x @context; # may be unnecessary $value= escape_xml( strip_quotes( $value)); print qq{<$attribute>$value</$attribute>\n}; } elsif( m{^\s*Private Sub (\S*)\s*\(([^)]*)\)\s*$}) { # Private Sub sub(args) my( $sub, $args)= ( $1, escape_xml($2)); print $INDENT x @context; # may be unnecessary print qq{<$SUB_TAG name="$sub" args="$args">\n}; push @context, $SUB_TAG; } elsif( m{^\s*End Sub\s*$}) { # End Sub pop @context; print $INDENT x @context; # may be unnecessary print qq{</$SUB_TAG>\n}; } elsif( @context && ($context[-1] eq $SUB_TAG)) { # print everything within a sub # the 3 cases dealing with subs should probably be before # any other case, depending on the syntax of the subs # but it is easier to understand the code this way print $INDENT x @context; print escape_xml($_); } elsif( !@context && m{^\s*VERSION\s+(\S+)\s*$}) { # VERSION line my $version= escape_xml( $1); print qq{<$FORM_TAG version="$version">\n}; } } print qq{</$FORM_TAG>\n}; # strips quotes from a quoted string # could probably be made faster by working directly on $_[0] sub strip_quotes { my $string= shift; if( (substr( $string, 0, 1) eq '"') && (substr( $string, -1, 1) eq '"') ) { substr( $string, 0, 1)= ''; substr( $string, -1, 1) = ''; } return( $string); } # escape characters that would render the XML invalid sub escape_xml { my $string= shift; $string=~ s{&}{&}g; $string=~ s{<}{<}g; $string=~ s{"}{"e;}g; # only needed for attributes return $string; } __DATA__ VERSION 5.00 Begin VB.Form Form1 Caption = "Form1" ClientHeight = 6465 ScaleHeight = 6465 ScaleWidth = 11715 StartUpPosition = 3 'Windows Default Begin VB.Frame main_frame Caption = "Frame1" Height = 8775 Left = 0 TabIndex = 0 Top = 360 Width = 10695 Begin VB.TextBox sub_category_creator_text Height = 285 Left = 7320 TabIndex = 8 Top = 1680 Width = 1815 End Begin VB.TextBox category_creator_text Height = 285 Left = 2040 TabIndex = 6 Top = 1680 Width = 2295 End Begin VB.TextBox title_text Height = 285 Left = 2040 TabIndex = 2 Top = 600 Width = 4695 End Begin VB.Label reference_number Alignment = 1 'Right Justify Caption = "Reference Number" Height = 375 Left = 360 TabIndex = 3 Top = 1200 Width = 1575 End Begin VB.Label TitleLabel Alignment = 1 'Right Justify Caption = "Title*:" Height = 375 Left = 360 TabIndex = 1 Top = 600 Width = 1575 End End End Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Private Sub Label1_Click() End Sub Private Sub Title_Click() End Sub Private Sub Text2_Change() End Sub Private Sub question_text_Change() Haidee < Ho! End Sub
|
|---|