Roland Kapl > XML-Table2XML-1.4 > XML::Table2XML



Annotate this POD

View/Report Bugs
Module Version: 1.4   Source  


XML::Table2XML - Generic conversion of tabular data to XML by reverting Excel's flattener methodology.


        use XML::Table2XML;
        my $outXML = "";
        # first parse column path headers for attribute names, id columns and special common sibling mark ("//")
        parseHeaderForXML("rootNodeName", ['/@id','/@name2','/a']);
        # then walk through the whole data to build the actual XML string into $outXML
        my @datarows = ([1,"testName","testA"],
        for my $lineData (@datarows) {
        #finally finish the XML and reset the static vars
        print $outXML;
        # yields:
        # <?xml version="1.0"?>
        # <rootNodeName id="1" name2="testName"><a>testA</a><a>testB</a><a>testC</a></rootNodeName>


table2xml is an algorithm having two functions that allow the conversion of tabular data to XML without using XSLT. This is achieved by reverting the "Flattener" methodology used by Microsoft Excel to convert the XML tree format to a two-dimensional table (see Opening XML Files in Excel and INFO: Microsoft Excel 2002 and XML).

This reversion is achieved by:

1. (possibly) modifying the flattened table a bit to enable a simpler processing of the data,

2. sequentially processing the data column- and row wise.

The whole algorithm is done without the aid of any XML library, so it lends itself to easy translation into other environments and languages.

Producing the XML:

1. invoke parseHeaderForXML, using a line with the rootnode and path information.

2. After parsing the header info, the table data can be processed row by row by calling addXMLLine. The current data row is provided in the single argument lineData, the built XML is string returned and can be collected/written.

3. A final call to addXMLLine with lineData == undef restores the static variables and finalizes the XML string (closes any still open tags).

Public Functions

parseHeaderForXML ($rootNodeName,\@header,$LINEBREAKS,$XMLDIRECTIVE,$ENCODING)

rootNodeName is the name of the common root node. Any /@rootAttributes and /#text will be placed under respectively after this root node.

header is a list of paths denoting the "place" of the data in the targeted XML. Following special cases are allowed:

LINEBREAKS specifies whether '\n' should be added after each datarow for easier readablity, default is no linebreaks

XMLDIRECTIVE specifies any header being inserted before the root element, default is '<?xml version="1.0"?'>.

ENCODING denotes the Unicode Codification used to encode the string(s) returned by addXMLLine(), default is 'iso-8859-1'

$returnedXML = addXMLLine(\@lineData)

lineData is a list of data elements that are converted to XML following the parsed header information.

The produced XML is returned as a function value which can be concatenated or written to a file. Bear in mind that the returned XML is just a part of a larger structure, so only after the last line has been processed and addXMLLine(undef) has been called, the XML structure is finished.

Prerequisites for column order and data layout

The layout of the columns (header = "data paths" and respective column data below) has to follow a certain layout:


Generally, pay close attention to the ordering of columns and constraints on the data as described above, since the algorithm in writeLine doesn't check for validity, thus producing invalid XML in case of failing to follow preparation steps correctly.

In mixed content nodes, the only way to correctly (re)produce the XML is for ONE content being right after the node name. There's currently no way to produce mixed content nodes with more than one text node (e.g., <node>text1<subnode>Test</subnode>text2</node> and the like).

Same sequential parent nodes are "factored" out by the flattener, so the unflattening algorithm treats them as being factored out, which means there is no way to exactly reproduce (<a><b>test1</b></a><a><b>test2</b></a>, this would be processed as <a><b>test1</b><b>test2</b></a>, which is semantically equal, but not the same...).


for a detailed discussion of the flattening algorithmm in Excel see and


Roland Kapl,


Copyright (C) 2006 by Roland Kapl

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.

syntax highlighting: