java 使用xom对象数据序列化为xml、反序列化、Preferences相关操作小案例

package org.rui.io.xml;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;
/**
 * 序列化到XML中
 * 使用xom来产生被转换为xml的Element对象的  person数据
 * 
 *http://www.xom.nu/
 * XOM虽然也是一种面向对象的XML API,类似于DOM的风格,但是它有一些与众不同的特性,比如严格保持内存中对象的不变性,
 * 从而使XOM实例总是能序列化为正确的XML。此外,与其他Java XML API相比,XOM追求更简单和更正规。
 * 
 *
 */
public class Person {
	
	private String first,last;
	public Person(String first,String last)
	{
		this.first=first;
		this.last=last;
	}
	
	//转换当前对像为xml
	public Element getXml()
	{
		Element person=new Element("person");
		
		Element firstName=new Element("first");
		firstName.appendChild(first);
		person.appendChild(firstName);
		
		Element lastNaem=new Element("last");
		lastNaem.appendChild(last);
		person.appendChild(lastNaem);
		
		return person;
	}
//
	public Person(Element person)
	{
		first=person.getFirstChildElement("first").getValue();
		last=person.getFirstChildElement("last").getValue();
	}
//
	@Override
	public String toString() {
		return "Person [first=" + first + ", last=" + last + "]";
	}
//把doc序列化
	public static void format(OutputStream os,Document doc) throws Exception
	{
		Serializer serializer=new Serializer(os,"ISO-8859-1");
		serializer.setIndent(4);
		serializer.setMaxLength(60);
		serializer.write(doc);
		serializer.flush();		
	}
	///main
	public static void main(String[] args) throws Exception {
		//实列对象列表
		List<Person> list=Arrays.asList(new Person("Dr.Bunsen","heneydew"),
				//new Person("东方","不败"),
				new Person("ddd","ffff"),
				new Person("kkkk","jjjjj"));
		System.out.println(list);
		
		//构见xml
		Element root=new Element("people");
		for(Person p:list)
			root.appendChild(p.getXml());
		Document doc=new Document(root);
		format(System.out,doc);
		//序列化
		format(new BufferedOutputStream(
				new FileOutputStream("people.xml")),doc);
		
	}
}

/**
 [Person [first=Dr.Bunsen, last=heneydew], Person [first=东方, last=不败], Person [first=kkkk, last=jjjjj]]
<?xml version="1.0" encoding="ISO-8859-1"?>
<people>
    <person>
        <first>Dr.Bunsen</first>
        <last>heneydew</last>
    </person>
    <person>
        <first>东方</first>
        <last>不败</last>
    </person>
    <person>
        <first>kkkk</first>
        <last>jjjjj</last>
    </person>
</people>
 */



package org.rui.io.xml;
import java.io.File;
import java.util.ArrayList;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Elements;


/**
 * 反序列化xml
 * xom的方法都具有相当的自解释性,可以在xom文档中找到它们,
 * @author lenovo
 *
 */
public class People  extends ArrayList<Person>{
	public People(String fileName) throws Exception
	{
		Document doc=new Builder().build(new File(fileName));//打开读取文件
		Elements elements=doc.getRootElement().getChildElements();
		for(int i=0;i<elements.size();i++)
		{//转换xml为对象
			add(new Person(elements.get(i)));
		}
		
	}
	public static void main(String[] args) throws Exception {
		//String path="D:\\Users\\liangrui\\workspace\\thinking/";
		//People people=new People(path+"people.xml");
		People people=new People("people.xml");
		System.out.println(people);
	}

}
/**
[Person [first=Dr.Bunsen, last=heneydew], Person [first=ddd, last=ffff], Person [first=kkkk, last=jjjjj]]

 */

package org.rui.io.xml;
import java.util.prefs.Preferences;
/**
 *  Preferences API 提供一种系统的方法来处理用户的偏好设置信息,例如.保存用户设置, 记住某个文本框的最后一个值等.
Preferences 是一个可以为任意名字的键/值对. 值可以为布尔型,字符型, 其他简单的数据类型,
如int. Preferences 通过get和set来获取和设置偏好信息,且get的方法可设置一个默认值,当要获取的键未被设置值时,就返回此默认值.
1.2. 数据的实际存储
数据的实际存储是依赖于操作系统平台的, 例如.在Windows 下面是使用注册表来保存这些信息,
而在Linux下面是使用用户的home目录下面的一个隐藏文件来存储的.
2. API使用
java.util.prefs.Preferences 很容易使用. 你不得不定义一个节点来存储数据. 
接下来就可以使用get和set的方法. 第二个参数是默认值,即当找不到值时,得到的就是这个默认值了, 
例如. 如果preference的值还未设置, 那么将会返回这个默认值.
 * @author lenovo
 *
 */

public class PerferencesDemo {
	public static void main(String[] args) throws Exception {
		
		Preferences perfs=Preferences.userNodeForPackage(PerferencesDemo.class);
		perfs.put("location", "0z");
		perfs.put("Footwear","Ruby Slippers");
		perfs.putInt("Companions",4);
		perfs.putBoolean("Are there witches?", true);
		int usageCount=perfs.getInt("UsageCount", 0);
		usageCount++;
		//System.out.println(usageCount);
		perfs.putInt("UsageCount", usageCount);
		for(String key:perfs.keys())
			System.out.println(key+":"+perfs.get(key, null));
			System.out.println("How many companions does Dorothy have?  "+
			perfs.getInt("Companions", 0));
		
		
	}

}/*
location:0z
Footwear:Ruby Slippers
Companions:4
Are there witches?:true
UsageCount:5
How many companions does Dorothy have?  4
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值