使用xstream读取固定xml中的方法

前言

用java读取xml中内容的方法有很多,其中最为著名的是通过@XmlElement方式读取(作者之前的博客中曾对此进行过总结,各位可以参考),这个适用于结构相对比较简单且内容较为庞大的xml,对于湘桂比较娇小的xml文件,更多的采用的适XStream方式读取。本文也是围绕这个展开。

要读取的xml内容

<?xml version="1.0" encoding="UTF-8"?>
<schools formatVersion="1.2">
    <school name="beijing_school_1" location="haidian" firstSearch="true"/>
    <school name="beijing_school_2" location="changping" firstSearch="true"/>
    <school name="beijing_school_3" location="yuyang" firstSearch="true"/>
</schools>

从上面xml中的内容可以看出来,是一个学校的集合,集合中共包含了3个学校。为了容下各个school的内容,首先需要针对单个学校创建Bean,而后再创建综合的schools的bean。

 

上述xml需要在web工程的配置文件中指明,此处实在applicationContext.xml文件中指明,代码为

 <bean id="schoolService" class="com.*.service.SchoolService" init-method="init"
          p:schoolFile="/WEB-INF/schools.xml"/>

Bean的创建

   school Bean的创建

   和xml中的一样,school也需要包含3个变量:1. name;2. location; 3. firstSearch。 talk is cheap ,请各位参考代码

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

/**
 * 
 * 用于读取school.xml中的内容
 */
public class School {
	
	@XStreamAsAttribute
	@XStreamAlias("name")
	private String name;
	
	@XStreamAsAttribute
	@XStreamAlias("location")
	private String location;
	
	@XStreamAsAttribute
	@XStreamAlias("firstSearch")
	private boolean fs;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public boolean isFs() {
		return fs;
	}

	public void setFs(boolean fs) {
		this.fs = fs;
	}

	@Override
	public String toString() {
		return "School [name=" + name + ", location=" + location + ", fs=" + fs + "]";
	}

}

Schools Bean的创建

该Bean用于收集所有单个school信息,代码为


import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("schools")
public class Schools {
	
	@XStreamImplicit(itemFieldName = "school")
	private List<School> schools;
	
	@XStreamAsAttribute
	@XStreamAlias("formatVersion")
	private String formateVersion;

	public List<School> getSchools() {
		return schools;
	}

	public void setSchools(List<School> schools) {
		this.schools = schools;
	}

	public String getFormateVersion() {
		return formateVersion;
	}

	public void setFormateVersion(String formateVersion) {
		this.formateVersion = formateVersion;
	}

	@Override
	public String toString() {
		
		StringBuilder sb = new StringBuilder();
		
		for(School sch: this.schools) {
			sb.append(sch.toString());
		}
		
		return sb.toString();
	}

}

读取和写入

下面代码中,在init()函数中将上述xml中的内容读入到内存,而在saveSchool()函数中将修改后的值再写入到xml中

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

import org.springframework.core.io.Resource;
import com.*.bean.place.Schools;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class SchoolService {
	
	private Schools schools;
	
	private Resource schoolFile;

	public void setSchoolFile(Resource schoolFile) {
		this.schoolFile = schoolFile;
	}
	
	void init() {
		XStream stream = new XStream(new DomDriver());
		stream.processAnnotations(Schools.class);
		
		InputStream input = null;
		Reader reader = null;
		try {
			input = this.schoolFile.getInputStream();
			reader = new InputStreamReader(input,"UTF-8");
			this.schools = (Schools)stream.fromXML(reader);
			System.out.println(this.schools);
			this.saveSchool(this.schools, schoolFile);
		}catch(Exception e) {
			System.out.println(e.toString());
		}
		finally {
			try {
				if(null != input) {
					input.close();
				}
			}catch(Exception e) {
				
			}
		}
		
	}
	
	
	public void saveSchool(Schools school, Resource resource) {
		XStream stream = new XStream(new DomDriver());
		stream.processAnnotations(Schools.class);
		
		Writer writer = null;
		OutputStream output = null;
		try {
			output = new FileOutputStream(resource.getFile());
			System.out.println("resource.getFile=" + resource.getFile());
			writer = new OutputStreamWriter(output,"UTF-8");
			
			writer.write("<?xml version=\\\"1.8\\\" encoding=\\\"UTF-8\\\"?>");
			//writer.write("good morning");
			writer.write(System.getProperty("line.separator"));//换行符
			school.setFormateVersion("2.3");
			stream.toXML(school,  writer);
			
			//System.out.println("+++++++hello  hello  hello++++++++++++++++");
			
		}catch(Exception e) {
			System.out.println("Exception " + e);
		}
		
		finally {
			if(null != writer) {
				try {
					writer.flush();
					writer.close();
				}catch(Exception e) {
					System.out.println("this is a exception -===");
				}
				
			}
			
			if(null != output) {
				try {
					output.flush();
					output.close();
				}catch(Exception e) {
					System.out.println("this is a exception -=------=");
				}
			}
			
			
		}
	}
	

}

修改后值

<?xml version=\"1.8\" encoding=\"UTF-8\"?>good morning
<schools formatVersion="2.3">
  <school name="beijing_school_1" location="haidian" firstSearch="true"/>
  <school name="beijing_school_2" location="changping" firstSearch="true"/>
  <school name="beijing_school_3" location="yuyang" firstSearch="true"/>
</schools>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值