解析XML文件

来源:互联网 发布:ubuntu 14.04 反应慢 编辑:程序博客网 时间:2024/06/01 08:19
解析XML文件
1.新建一个Cat类,其中包含一个带参的构造方法

publicclass Cat {

         publicCat(String name){

                   System.out.println("new Cat : " + name);

         }

}

2.写一个 通过反射机制 来获取 类的构造方法 的函数
         // className:类名,pa1:带参构造方法的实参
         publicstaticvoid 获取构造方法(String className, String pa1) {
                   // 1.获取类
                   try {
                            Class<?>forName = Class.forName(className);
 
                            // 2.获取构造方法
                            Constructor<?>cs = forName.getConstructor(String.class);
 
                            //实例化对象
                            @SuppressWarnings("unused")
                            Object obj =cs.newInstance(pa1);
 
                   } catch (ClassNotFoundException |NoSuchMethodException
                                     |SecurityException | InstantiationException
                                     |IllegalAccessException | IllegalArgumentException
                                     |InvocationTargetException e) {
                            e.printStackTrace();
                   }
 

         }

3.在主方法中解析XML文件,并调用步骤2中写好的函数

publicstatic void main(String[] args) {

                   /**
                    * 解析XML文件
                    */
                   Element e = null;
                   //可使用绝对路径
                   File f = new File("src/config.xml");
 
                   try {
                            // doucumentBuilder为抽象类,不能直接实例化(将XML文件转换为DOM文件)
                            DocumentBuilderFactorydbf = DocumentBuilderFactory.newInstance();
                            DocumentBuilder db =dbf.newDocumentBuilder();
                            Document dt =db.parse(f);//得到一个DOM
 
                            e =dt.getDocumentElement();
                            String nodeName= e.getNodeName();//获取根元素名称
 
                            NodeList childNodes= e.getChildNodes();//获取根元素下的所有子节点
 
                            //遍历这些子节点
                            for (int i = 0;i < childNodes.getLength(); i++) {
                                     //获取每个对应位置i的节点
                                     Node item =childNodes.item(i);
                                     if ("bean".equals(item.getNodeName())){
                                               StringsClass = item.getAttributes().getNamedItem("class")
                                                                 .getNodeValue();
 
                                               //获得<Accounts>下的节点
                                               NodeListnodeDetail = item.getChildNodes();
                                               //遍历<Accounts>下的节点
                                               for (int j = 0;j < nodeDetail.getLength(); j++) {
                                                        //获得<Accounts>元素的每一个节点
                                                        Nodedetail = nodeDetail.item(j);
                                                        if ("property".equals(detail.getNodeName())) {
                                                                 StringsV = detail.getAttributes()
                                                                                    .getNamedItem("value").getNodeValue();
                                                                 获取构造方法(sClass,sV);
                                                        }
                                               }
                                     }
 
                            }
 
                   } catch(ParserConfigurationException | SAXException | IOException e1) {
                            e1.printStackTrace();
                   }

 

         }




原创粉丝点击