Creating KVTML Files


KWordQuiz, KVocTrain, and other KDE-based programs use the KVTML file format for their data files. The format is just a fairly simple XML format but, unfortunately, there doesn't seem to be anything available to convert a text file to this format. So, once again, AWK to the rescue.

While I was extracting data from a fairly convoluted HTML file which required some more awk, the generic idea was to take some easy-to-make text format and build the needed KVTML file. I decided to use one line per record with a "|" as the field separator. There is nothing magic about using this character—you just need something that does not appear in the data itself.

My "knowledge" of the KVTML format comes from creating a file with KWordQuit and then taking a look at it. I don't really understand with rows and columns is used for but it doesn't seem important. Without further ado, here is the awk program.

# -- convert |-separated lines into a kvtml file
# invoke as follows:
#   awk -f [l1=first_label] [l2=second_label] filename(s)
#       l1 and l2 are optional column labels

        l1 = "Column 1"         # default labels
        l2 = "Column 2"
        FS = "|"                # field separator
        print "<?xml version=\"1.0\"?>"
        print "<!DOCTYPE kvtml SYSTEM \"kvoctrain.dtd\">"
        print "<kvtml"
        print "generator=\"\""
        print "cols=\"2\""
        print "lines=\"50\""

first   {               # output header with first line
        nfn = FILENAME
        sub(/\..*$/, ".kvtml", nfn)
        print "title=\"" nfn "\">"
        first = 0
        print " <e>"
        print "  <o width=\"250\" l=\"" l1 "\">" $1 "</o>"
        print "  <t width=\"250\" l=\"" l2 "\">" $2 "</t>"
        print " </e>"


        {               # all subsequent lines
        print " <e>"
        print " <o>" $1 "</o>"
        print " <t>" $2 "</t>"
        print " </e>"

END     {
        print "</kvtml>"

The BEGIN block in the awk script is executed before the input file is opened. First it sets default column labels. They can be overridden on the command line. It sets the field separator (FS) to "|" and outputs most of the boilerplate. It does not, however, output the line with the filename in it as I create that filename from the input filename (replacing everything after a dot with ".kvtml". This has to be done later as FILENAME is not yet set. The variable first is set to indicate this remaining work is yet to be done.

The code executed if first is set outputs the remainder of the boilerplate plus the first record data along with the column tags—either those supplied on the command line or the defaults that were set in the BEGIN block. Finally, first is set to zero so this block will not be executed again. The next statement causes awk to skip to the next record rather than continuing processing on the current one.

For each subsequent input line, the default (no condition) block of code is executed. When the end of file is reached, the END block of code is executed.

Output is sent to standard output. You can redirect it to wherever you want. You can change the value of FS and RS to handle different imput formats. If you need to process an input format that requires a bit more processing, just add a unconditional block right after the BEGIN block that massages the data into a reasonable format. Judicious use of getline and next along with sub and gsub functions should be able to handle most anything.


Phil Hughes


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Hi, For some reason I get an

Tony Adams's picture


For some reason I get an error on the line:

sub(/\..*$/, ".kvtml", nfn)

What is that line actually for? Thanks.
- hp 6735s

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState