Convert XML to Office Document

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
nsamy
Posts: 2
Joined: Thu Jan 03, 2013 11:18 am

Convert XML to Office Document

Post by nsamy »

Hi Team,

I am new to open office and I have installed Open Office, Now I need to convert an XML to office document with OpenOffice API. Please guide me.\

Thanks.
OpenOffice 3.1 on Windows Vista / NeoOffice 2.2.3 with MacOS 10.4 / OpenOffice 2.4 on Ubuntu 9.04
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Convert XML to Office Document

Post by Villeroy »

Write an XSLT script to convert your XML to the correct ODF file type and install your script under Tools>"XML Filters". Then you simply open your files and save it as ODF file.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
nsamy
Posts: 2
Joined: Thu Jan 03, 2013 11:18 am

Re: Convert XML to Office Document

Post by nsamy »

Hi Villeroy, Thanks for your reply. Please elaborate the above process.
Since I am new to XSLT, Please let me know which tool should I use to write XSLT script ?
Should I save XML file as ODF file ?
OpenOffice 3.1 on Windows Vista / NeoOffice 2.2.3 with MacOS 10.4 / OpenOffice 2.4 on Ubuntu 9.04
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Convert XML to Office Document

Post by Villeroy »

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Convert XML to Office Document

Post by rudolfo »

If you want to work with XSLT make sure you have the XSLT filters installed. You need to explicitly select them in customized installation mode, they won't get installed by default.
The XSLT processing in OpenOffice is based on Java tools, so it requires a correct setup of the Java Runtime (On MS Windows Java 6 and 32bit is working reliably with OOo, a 64bit Java cannot work with OOo).
If you have never seen an XSLT file before, I recommend to have a look at the docbook export/import Filter. It can be found inthe $OOoINSTALLDIR/Basis/share/xslt/docbook directory as the file sofftodocbookheadings.xsl (read it as StarOffice2docbook). Look at it together with a good Tutorial about XSLT.
You may probably follow the second .xsl file in that directory for importing/converting generic XML to ODF, but the export filter sofftodocbookheadings.xsl has more explaining comments.
If you are looking for a tool Apache Ant or if you want to go with a full blown IDE Eclipse might be helpful.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
User avatar
jza
Posts: 239
Joined: Mon Nov 03, 2008 11:33 am
Location: Cancún, Mexico

Re: Convert XML to Office Document

Post by jza »

I remember there was an XSLT template, so that users dont need to go through the whole spec of the Spreadsheet, this was used to convert to HTML, DocBooks and others. I cant remember where exactly it was.
There are some basic, python and java samples as well on using the XML parsing features.
http://incubator.apache.org/odftoolkit/ ... unner.html and http://books.evc-cit.info/odbook/apb.html

Code: Select all

<xsl:template match="document">
    <html>
        <head>
            <title>My Document</title>
        </head>
        <body>
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>

<xsl:template match="para">
    <p>
        <xsl:apply-templates/>
    </p>
</xsl:template>

<xsl:template match="index">
    <h1>Index</h1>
    <ul>
    <xsl:apply-templates/>
    </ul>
</xsl:template>

<xsl:template match="item">
    <li>
        <xsl:apply-templates/>
    </li>
</xsl:template>

<xsl:template match="endnote">
    <h3><xsl:apply-templates/></h3>
</xsl:template>
here is some python script interacitng with ODS:
http://books.evc-cit.info/odbook/ch05.h ... et-section

Code: Select all

import xml.dom 
import xml.dom.ext
import xml.dom.minidom
import xml.parsers.expat
import sys
import od_number
from zipfile import *
from StringIO import *

if (len(sys.argv) == 4):
    
    #   Open an existing OpenDocument file
    #
    inFile = ZipFile( sys.argv[1] )
    
    #   ...and a brand new output file
    #
    outFile = ZipFile( sys.argv[2], "w", ZIP_DEFLATED );
    
    getParameters( sys.argv[3] )
    
    #
    #   modify all appropriate currency styles
    #
    fixCurrency( "styles.xml" )
    fixCurrency( "content.xml" )

    #
    #   copy the manifest
    #
    copyManifest( )
    
    inFile.close
    outFile.close
else:
    print "Usage: " + sys.argv[0] + " inputfile outputfile parameterfile"
There is also the flat XML format although not sure if it can be used for spreadsheets.
AOO 4.1.1 on Arch Linux
Post Reply