xml解析和反射创建对象

直接上代码

XML:

<!--config.xml-->

<?xml version = "1.0">

<config>

    <className>ClassName<className>

<config>


public class XMLUtil{

    public static Object getBean() {

        try{

           DocumentBuiderFactory dFactory = DocumentBulderFactory.newInstance();

          DocumentBuider db = dFactory .newDocumentBuider();

          Document doc;

          doc = db.parse(new File("config.xml"));

           NodeList nl = doc.getElementsByTagName("className");

           Node className = nl .item(0).getFirstChild();

           String cName = classNode.getNodeValue();

           Class c = Class.forName(cName);

           Object obj = c.newInstance();

           return obj;

        } catch(Exception e) {

            e.printStackTrace();

            return null

        }

    }        

}

本文出自 “8404907” 博客,转载请与作者联系!

你可能感兴趣的:(xml解析和反射创建对象)