Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

public class Main {
    public static ArrayList<String> getFontSizeList() {
        ArrayList<String> fontList = new ArrayList<String>();
        DocumentBuilderFactory dbfFont = null;
        DocumentBuilder dbFont = null;
        Document domFont = null;

        try {
            dbfFont = DocumentBuilderFactory.newInstance();
            String fontSizeFileName = "resources/FontSizes.xml";

            //Using factory get an instance of document builder
            dbFont = dbfFont.newDocumentBuilder();

            //parse using builder to get DOM representation of the XML file
            domFont = dbFont.parse(fontSizeFileName);

            //get the root elememt
            Element docEle = domFont.getDocumentElement();

            //get a nodelist of <sizes> elements
            NodeList sizeList = docEle.getElementsByTagName("size");
            if (sizeList != null && sizeList.getLength() > 0) {
                for (int i = 0; i < sizeList.getLength(); i++) {

                    //get the employee element
                    Element sizeElement = (Element) sizeList.item(i);
                    fontList.add(sizeElement.getTextContent());
                }

            }
        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return fontList;

    }
}