本文介绍两种从本地的xml文件中读取数据,并把相关的数据封装到list中的方法,要解析xml,需要先下载dom4j-1.6.1.jar
第一种方法:
首先建立students.xml,保存在E盘下面:
------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<students>
<student sn = "01" sm = "11">
<name>张三</name>
<age>15</age>
</student>
<student sn = "02" sm = "22">
<name>李四</name>
<age>16</age>
</student>
</students>
--------------------------------------------------------------------------------------------------
然后建立辅助类:
该类的作用是封装从xml中读出的信息,包括sn,sm,name,age
--------------------------------------------------------------------------------------------------
package com.bsj.cityelife;
public class Attributes {
private String sn;
private String sm;
private String name;
private String age;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getSm() {
return sm;
}
public void setSm(String sm) {
this.Sm = sm;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
-----------------------------------------------------------------------------------------------------
建立解析xml的类:
该类的作用是解析xml文件,把其中的信息先封装到attributes类中,然后把多个attributes实例封装到list中
------------------------------------------------------------------------------------------------------
package com.bsj.cityelife;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.ProcessingInstruction;
import org.dom4j.VisitorSupport;
public class MyVisitor extends VisitorSupport {
static ArrayList list = new ArrayList();
Attributes a;
public void visit(Attribute node) {
if ("sn".equals(node.getName())) {
a = new Attributes();
// System.out.println("attribute"+node.getName()+"="+node.getValue());
a.setName1(node.getValue());
//System.out.println(a.getName1());
} else {
// System.out.println("attribute"+node.getName()+"="+node.getValue());
// System.out.println(a.getName1());
a.setName2(node.getValue());
}
}
public void visit(Element node){
if (node.isTextOnly()){
if (node.getName().equals("age")){
a.setName4(node.getText());
list.add(a);
}else{
a.setName3(node.getText());
}
}
}
public static ArrayList print() {
return list;
}
}
---------------------------------------------------------------------------------------------------------------------------
调用MyVisitor的类:
该类调用MyVisitor类,并得到结果集list
---------------------------------------------------------------------------------------------------------------------------
package com.bsj.cityelife;
import java.io.File;
import java.util.ArrayList;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
public class VisitorTest {
public static void main(String[] args){
SAXReader saxReader = new SAXReader();
File file = new File("E://students.xml");
try{
Document doc = saxReader.read(file);
doc.accept(new MyVisitor());
}catch(DocumentException e){
e.printStackTrace();
}
ArrayList list = MyVisitor.print();
for(int i=0;i<list.size();i++){
Attributes a = (Attributes) list.get(i);
System.out.println("--------------------------------");
System.out.println(a.getName1());
System.out.println(a.getName2());
System.out.println(a.getName3());
System.out.println(a.getName4());
//System.out.println(a.getName2());
//System.out.println(a.getName3());
System.out.println("--------------------------------");
}
}
}
------------------------------------------------------------------------------------------------------------------------------------
第二种方法:
这个方法只能读取指定元素的属性,如要读取其它内容,还需改进,但这个方法相对比较简洁
如上,先建立xml,及attributes.java这两个文件,然后通过以下代码读取:
-------------------------------------------------------------------------------------------------------------------------------
package com.bsj.cityelife;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
public class VisitorTest {
public static void main(String[] args) throws DocumentException{
SAXReader saxReader = new SAXReader();
File file = new File("E://students.xml");
Document doc = saxReader.read(file);
Element root=doc.getRootElement();
List citys=new ArrayList();
List elements=root.selectNodes("/students/student");
for(Iterator $it=elements.iterator();$it.hasNext();){
Element element=(Element)$it.next();
String name=element.attributeValue("sn");
String prefix=element.attributeValue("sm");
Attributes a = new Attributes();
a.setSn(name);
a.setSm(prefix);
citys.add(a);
//System.out.println(name);
//System.out.println(prefix);
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------