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 非常easy使用. 你不得不定义一个节点来存储数据. 
接下来就能够使用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
*/

猜你喜欢

转载自www.cnblogs.com/ldxsuanfa/p/10656702.html