Spreadsheet to XML

Listing 3. The ExcelToXML.java application converts an Excel spreadsheet to an XML document.

import org.apache.poi.hssf.usermodel.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class ExcelToXML {
  public void generateXML(File excelFile) {
    try {
      DocumentBuilderFactory factory = 
        DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = 
        factory.newDocumentBuilder();
      Document document = builder.newDocument();
      Element catalogElement = document.createElement(
        "catalog");
      document.appendChild(catalogElement);

      InputStream input = new FileInputStream(
        excelFile);
      HSSFWorkbook workbook = new HSSFWorkbook(input);
      HSSFSheet spreadsheet = workbook.getSheetAt(0);

      for (int i = 0; i <= 
        spreadsheet.getLastRowNum(); i++) {
        HSSFRow row = spreadsheet.getRow(i);

        Element journalElement = 
          document.createElement("journal");
        catalogElement.appendChild(journalElement);

        Element sectionElement = 
          document.createElement("section");
        journalElement.appendChild(sectionElement);
        sectionElement.appendChild(
          document.createTextNode(row.getCell((short)
          0).getStringCellValue()));

        Element publisherElement = 
          document.createElement("publisher");
        journalElement.appendChild(publisherElement);
        publisherElement.appendChild(
          document.createTextNode(
          row.getCell((short) 
          1).getStringCellValue()));

        Element levelElement = document.createElement(
          "level");
        journalElement.appendChild(levelElement);
        levelElement.appendChild(
          document.createTextNode(row.getCell((short) 
          2).getStringCellValue()));

        Element editionElement = 
          document.createElement("edition");
        journalElement.appendChild(editionElement);
        editionElement.appendChild(
          document.createTextNode(
          row.getCell((short) 
          3).getStringCellValue()));

        Element titleElement = document.createElement(
          "title");
        journalElement.appendChild(titleElement);
        titleElement.appendChild(
          document.createTextNode(row.getCell((short) 
          4).getStringCellValue()));

        Element authorElement = 
          document.createElement("author");
        journalElement.appendChild(authorElement);
        authorElement.appendChild(
          document.createTextNode(row.getCell((short) 
          5).getStringCellValue()));
      }

      TransformerFactory tFactory = 
        TransformerFactory.newInstance();

      Transformer transformer = 
        tFactory.newTransformer();

      DOMSource source = new DOMSource(document);
      StreamResult result = new StreamResult(new File(
        "C:/Excel/catalog.xml"));
      transformer.transform(source, result);
    } catch (IOException e) {
    } catch (ParserConfigurationException e) {
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
    }
  }

  public static void main(String[] argv) {
    ExcelToXML excel = new ExcelToXML();
    File input = new File(
      "C:/Excel/ExampleExcel.xls");
    excel.generateXML(input);
  }
}