zoukankan      html  css  js  c++  java
  • Java 解析自定义XML文件

    这里我用 maven项目 作为 演示

     

    配置pom.xml文件

    完整的pom.xml文件信息

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 
     3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5   <modelVersion>4.0.0</modelVersion>
     6 
     7   <groupId>com.oukele.Readerxml</groupId>
     8   <artifactId>ReaderXml</artifactId>
     9   <version>1.0-SNAPSHOT</version>
    10 
    11   <name>ReaderXml</name>
    12   <!-- FIXME change it to the project's website -->
    13   <url>http://www.example.com</url>
    14 
    15   <properties>
    16     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    17     <maven.compiler.source>1.7</maven.compiler.source>
    18     <maven.compiler.target>1.7</maven.compiler.target>
    19   </properties>
    20 <!--添加依赖-->
    21   <dependencies>
    22 
    23   <!--单元测试-->
    24     <dependency>
    25       <groupId>junit</groupId>
    26       <artifactId>junit</artifactId>
    27       <version>4.11</version>
    28       <scope>test</scope>
    29     </dependency>
    30 
    31     <!-- dom4j 依赖-->
    32     <dependency>
    33       <groupId>org.dom4j</groupId>
    34       <artifactId>dom4j</artifactId>
    35       <version>2.1.1</version>
    36     </dependency>
    37 
    38   </dependencies>
    39 
    40   
    41   <build>
    42     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
    43       <plugins>
    44         <plugin>
    45           <artifactId>maven-clean-plugin</artifactId>
    46           <version>3.0.0</version>
    47         </plugin>
    48         <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
    49         <plugin>
    50           <artifactId>maven-resources-plugin</artifactId>
    51           <version>3.0.2</version>
    52         </plugin>
    53         <plugin>
    54           <artifactId>maven-compiler-plugin</artifactId>
    55           <version>3.7.0</version>
    56         </plugin>
    57         <plugin>
    58           <artifactId>maven-surefire-plugin</artifactId>
    59           <version>2.20.1</version>
    60         </plugin>
    61         <plugin>
    62           <artifactId>maven-jar-plugin</artifactId>
    63           <version>3.0.2</version>
    64         </plugin>
    65         <plugin>
    66           <artifactId>maven-install-plugin</artifactId>
    67           <version>2.5.2</version>
    68         </plugin>
    69         <plugin>
    70           <artifactId>maven-deploy-plugin</artifactId>
    71           <version>2.8.2</version>
    72         </plugin>
    73       </plugins>
    74     </pluginManagement>
    75   </build>
    76 
    77 </project>
    View Code

    项目结构

    Cat类(Dog类 类似)截图:

    接口IBeans

    在main文件夹下面新建一个resources文件夹

     

    在resources文件夹中新建一个xml(文件名字自定义)文件

    比如:

    test_xml.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!--自定义 父节点-->
     3 <beans>
     4 
     5 <!--自定义 子节点-->
     6     <beans id="cat" class="com.oukele.Readerxml.animal.Cat" ></beans>
     7 
     8     <beans id="dog" class="com.oukele.Readerxml.animal.Dog" ></beans>
     9 
    10 </beans>
    View Code

    Beans类代码:

     1 package com.oukele.Readerxml.ioc;
     2 
     3 import org.dom4j.Document;
     4 import org.dom4j.DocumentException;
     5 import org.dom4j.Element;
     6 import org.dom4j.io.SAXReader;
     7 
     8 import java.net.URL;
     9 import java.util.HashMap;
    10 import java.util.List;
    11 import java.util.Map;
    12 
    13 public class Beans implements IBeans{
    14 
    15     private static Map<String,Object> banes = new HashMap<>();
    16 
    17     public Beans(String configxml) throws Exception {
    18         //创建SAXReader对象
    19         SAXReader reader = new SAXReader();
    20         //xml文件的位置
    21         URL sources = Beans.class.getClassLoader().getResource(configxml);
    22         //创建document对象,并读取xml文件 (解析xml文件)
    23         Document document = reader.read(sources);
    24         //读取元素   getRootElement() --》 获取父节点  elements() --> 所有节点
    25         List<Element> elements = document.getRootElement().elements();
    26 
    27         for (Element element : elements) {
    28 //       <beans id="cat" class="com.oukele.Readerxml.animal.Cat" ></beans>
    29 //        id 是一个属性  可以自定义
    30             String id = element.attributeValue("id");//获取id属性
    31             String clazz = element.attributeValue("class");//获取class属性
    32             //  id (钥匙)  对应 一个 实例
    33             banes.put(id,Class.forName(clazz).newInstance());
    34         }
    35 
    36     }
    37 
    38 
    39     @Override
    40     public Object getBean(String name) {
    41         //根据名字来获取实例
    42         return banes.get(name);
    43     }
    44 }
    View Code

    去test文件中的AppTest类中测试一下

     1 package com.oukele.Readerxml;
     2 
     3 import com.oukele.Readerxml.animal.Cat;
     4 import com.oukele.Readerxml.animal.Dog;
     5 import com.oukele.Readerxml.ioc.Beans;
     6 import org.junit.Test;
     7 
     8 public class AppTest 
     9 {
    10     @Test
    11     public void Test1(){
    12         try {
    13 
    14             Beans beans = new Beans("test_xml.xml");
    15 
    16             Cat cat = (Cat) beans.getBean("cat");
    17             cat.say();
    18             
    19             System.out.println("----------------------------------");
    20 
    21             Dog dog = (Dog) beans.getBean("dog");
    22             dog.say();
    23 
    24         } catch (Exception e) {
    25             e.printStackTrace();
    26         }
    27     }
    28 }
    View Code

    结果:

  • 相关阅读:
    jQuery radio的取值与赋值
    MVC中一般为什么用IQueryable而不是用IList?用IQueryable比IList好在哪?
    Git 的 .gitignore 配置
    XPath学习:轴(3)——descendant
    python遍历数组的两种方法
    selenium webdriver的各种driver
    Scrapy and Selenium
    scrapy和selenium结合抓取动态网页
    Python处理JSON
    Crontab定时任务配置
  • 原文地址:https://www.cnblogs.com/oukele/p/9754685.html
Copyright © 2011-2022 走看看