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>
项目结构
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>