Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static Object getBean() {
        try {
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document document;
            document = builder.parse(new File("config.xml"));

            NodeList nl = document.getElementsByTagName("StrategyClassName");
            Node classNode = nl.item(0).getFirstChild();
            String cName = classNode.getNodeValue();

            System.out.println(cName);
            Class c = Class.forName("com.seven.strategy.sort." + cName);
            Object object = c.newInstance();
            return object;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}