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

来源:互联网 发布:linux ov7725模块驱动 编辑:程序博客网 时间:2024/04/29 07:54
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;}//转换当前对像为xmlpublic 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();}//////////////////////////////@Overridepublic 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();}///////mainpublic 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);//构见xmlElement 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:0zFootwear:Ruby SlippersCompanions:4Are there witches?:trueUsageCount:5How many companions does Dorothy have?  4*/

2 0