I'm trying to write a tool that will convert an xls file into an xml file. I'm able to open and read the xls, but I'm unable to write the root element which will be pulled from the first cell in the first sheet. It only allows me to write it if it's from the last sheet in the workbook.

Code:
private void readCellsInSheet(Workbook wb, int num, String filePath) throws ParserConfigurationException, TransformerException
{
    //create sheet
    int sheetNum = num;
    Sheet sheet = wb.getSheetAt(sheetNum);

    //create document builder
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = null;
    //iterate through all the rows and cells
    String value;
    for(Row row : sheet)
    {
        for (Cell cell : row)
        {
            if (sheetNum == 0 && cell.getColumnIndex() == 0 && cell.getRowIndex() == 0)
            {
                doc = docBuilder.newDocument();
                value = cell.getRichStringCellValue().getString();
                Element root = doc.createElement(value);
                doc.appendChild(root);
            }
        }
    }
    sheetNum++;
    //call readCellsInSheet for every sheet in workbook
    if (wb.getNumberOfSheets() > sheetNum)
    {
        readCellsInSheet(wb, sheetNum, filePath);
    }
    else
    {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer optimusPrime = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        //StreamResult sr = new StreamResult(new File(filePath + ".xml"));
        StreamResult sr = new StreamResult(System.out);
        optimusPrime.transform(source, sr);
        return;
    }
}
also any constructive criticism on the code would be great!