1.建立test.xml文件
<books>
<book ID="1" name="java练习">
<author>Wmen</author>
<title>Java练习</title>
<publisher>哈哈哈</publisher>
</book>
<book ID="2" name="Xml解析">
<author>失眠忍者</author>
<title>Xml解析</title>
<publisher>啦啦啦</publisher>
</book>
<book ID="3" name="练习册">
<author>WJ</author>
<title>练习册</title>
<publisher>嘻嘻嘻</publisher>
</book>
</books>
2.建立testClass.java
3.利用getResourceAsStream方法读取test.xml文件并转换成流的形式:
InputStream ins = this.getClass().getResourceAsStream("test.xml");
4.用SAXReader().read(ins)将ins转换为Document:
document = new SAXReader().read(ins);
5.Element rootElt = document.getRootElement();转换成枚举类型以便于处理
6.迭代获得的枚举rootElt:
Iterator<Element> eltIterator = rootElt.elementIterator();
7.Element elt = eltIterator.next();自此已经可以获取元素值。
elt.getStringValue()即可
最后贴上源码包含转list等一下处理
项目目录结构(为maven项目)
相关jar包的引入:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
</dependencies>
testClass代码:
public void test(){
List<Map<String, String>> columnDatas = new ArrayList<Map<String, String>>();
InputStream ins = this.getClass().getResourceAsStream("test.xml");
System.out.println("将xml解析转换成流的形式:"+ins);
Document document = null;
try {
document = new SAXReader().read(ins);
System.out.println("SAXReader().read()解析后的document:"+document);
Element rootElt = document.getRootElement();
System.out.println("将document转成Element:"+rootElt);
Iterator<Element> eltIterator = rootElt.elementIterator();
System.out.println("迭代后:"+eltIterator);
while(eltIterator.hasNext()) {
Element elt = eltIterator.next();
System.out.println("取得元素"+elt.getStringValue());
List<Attribute> attributes = elt.attributes();
System.out.println("转list:"+attributes);
Map<String, String> cd = new HashMap<String, String>();
for (Attribute attr : attributes) {
//保存xml里面属性属于这个TransXMLDTO里面
String name = StringUtils.trim(attr.getName());
System.out.println("属性名称"+name);
// TransXMLDTD.valueOf(name);
cd.put(name, StringUtils.trim((String) attr.getData()));
}
columnDatas.add(cd);
System.out.println(columnDatas.get(0));
}
} catch (DocumentException e) {
e.printStackTrace();
} finally {
try {
if (null != ins) {
ins.close();
}
} catch (IOException e) {
logger.debug("流关闭失败");
throw new RuntimeException("流关闭失败");
}
}
System.out.println(this.getClass().getSimpleName());
}
利用Junit进行简单的测试
package controller;
import org.junit.Test;
public class testClassSon extends testClass {
@Test
public void testSon(){
test();
}
}
运行后结果为
注意:若用idea进行开发,需设置test.xml路径,因为eclipse和idea的编译读取文件时的处理不一样,若不设置
this.getClass().getResourceAsStream("test.xml")
处会读取不到文件。
Maven项目下可在pom.xml配置添加如下代码即可读取到
<build>
<finalName>WjTest</finalName>
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
</build>