⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.java

📁 一个“对象--XML 映射”(Object-Xml Mapping) 的类库。 它的目的是帮助开发者方便、快速的从XML 文件构建出Java 对象
💻 JAVA
字号:
/**
 * @author 沈东良 Edward Shen<a href="mailto:shendl_s@hotmail.com">shendl_s@hotmail.com</a>
 * 2007-8-15 上午11:54:18
 */
package net.sf.oxmled.mapping.sample;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;

import javax.management.AttributeList;

import net.sf.oxmled.mapping.annotation.XMLAttribute;
import net.sf.oxmled.mapping.annotation.XMLNode;
import net.sf.oxmled.mapping.service.IConvertCastUtil;
import net.sf.oxmled.mapping.service.IOXmlMapping;
import net.sf.oxmled.mapping.service.OXmlMapping;
import net.sf.oxmled.xml.util.dom4j.IXmlUtil;
import net.sf.oxmled.xml.util.dom4j.XmlUtil;

/**
 * @author 沈东良 Edward Shen<a href="mailto:shendl_s@hotmail.com">shendl_s@hotmail.com</a>
 * 2007-8-15 上午11:54:18
 *
 */
/**
 * 1,根节点,应该指定为root。
 * 2,如果编码是默认的utf-8,那么属性值可以是中文,但属性的名字,节点的名字等如果是中文,就会是乱码。
 * 因此,设为gbk。
 *    只有根节点需要在类定义上标注XMLNode,如果是作为子节点,则不需要在类上进行标注。 可以在属性上添加标注!
 *    
 *    注意:在基类上声明属性是无用的!  以为无法反射得到那些私有字段!
 */
@XMLNode(root=true,encoding="gbk")
public class Test {
	/**
	 * 属性。
	 * 指定了从xml文件的属性值转到Java类的字段的值的方法。 通过这个方法,可以根据XML文件的相应属性的String值,得到java对象中
	 * 该属性的实际的int值。
	 */
	@XMLAttribute(castMethod=IConvertCastUtil.castInt)
	private int a=1;
	/**
	 * 上面的castMethod=IConvertCastUtil.castInt 并没有实际指定使用这个方法。而是用了一个专门的工具类的方法。
	 *   查找的顺序是,那个工具类的方法,如果没有,就找本地的方法。
	 *   如果上面的Annotation改为这样:
	 *  @XMLAttribute(castMethod="castInt") 
	 *  就会使用这个方法进行转换。
	 * @param value
	 * @return
	 */
	private int castInt(String value){
		return Integer.parseInt(value);
		
	}
	/**
	 * 指定这个字段是需要映射为XML文件的数据项
	 */
	@XMLAttribute()
	private String b="BBB";
	@XMLAttribute(castMethod="castBoolean")
	private boolean bool=false;
	/**
	 * 
	 * @param value
	 * @return
	 */
	private boolean castBoolean(String value){
		
		return Boolean.parseBoolean(value);
	}
	
	@XMLAttribute(castMethod="castDouble")
	private double dd=245.32d;
	/**
	 * 
	 * @param value
	 * @return
	 */
	private double castDouble(String value){
		return Double.parseDouble(value);
	}
	
	/**
	 * 这个属性,保存在xml文件中时,使用“我们”作为属性的名字。
	 */
	@XMLAttribute(name="我们",castMethod="castInt")
	private int aa=1;
	/**
	 * 这个属性,保存在xml文件中时,使用"www"作为属性的名字。
	 */
	@XMLAttribute(name="www")
	private String bb="BBB";
	@XMLAttribute(value="true",castMethod="castBoolean")
	private boolean boolbbb=false;
	/**
	 * 这个属性,保存在xml文件中时,使用"dddda"作为属性的名字。使用固定的"111.32d"作为属性的值
	 */
	@XMLAttribute(name="dddda",value="111.32d",castMethod="castDouble")
	private double ddd=245.32d;

	
	@XMLAttribute
	private String str="String";
	/**
	 * 子节点  可以是一个单独的类,也可以是一个数组,一个Collection接口的实现类!
	 *    
	 */
	@XMLNode()
	private TestB testB=new TestB();
	
	@XMLNode()
	private TestB[] child=new TestB[]{new TestB(),new TestB(),null,new TestB(),null,new TestB()};
	/**
	 * 子节点
	 */
	@XMLNode()
	private AttributeList childList=new AttributeList();
	

	/**
	 * 
	 */
	public Test() {
		/*
		 *
		 */
		this.testB.setSsss("I love U!");
		this.childList.add("sssssssss");
		this.childList.add(new TestB());
	}

	/**
	 * @param args
	 * @throws Exception 
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		/*
		 *
		 */
		//IXmlUtil util=new XmlUtil();
		//util.loadXmlFile(ClassLoaderUtil.getResource("net.sf.oxmled.mapping.sample.Test"));
		
		Test test=new Test();
		System.out.println(test.a);
		//这是执行Object-XML Mapping的助手类
		IOXmlMapping oXmlMapping=new OXmlMapping();
		try {
			//把java对象生成为一个xml文件。 这里会放在程序的根路径下。名字是该对象的类名。
			//你也可以指定生成的位置和文件名。
		 	oXmlMapping.save(test);
		 	/**
		 	 * load方法,根据xml文件,生成Java对象!
		 	 */
			Test test2 =(Test) oXmlMapping.load("net.sf.oxmled.mapping.sample.Test");
			System.out.println(test2.a);
			System.out.println(test2.aa);
			
		} catch (IllegalArgumentException e) {
			/*
			*
			*/
			e.printStackTrace();
		} catch (MalformedURLException e) {
			/*
			*
			*/
			e.printStackTrace();
		} catch (URISyntaxException e) {
			/*
			*
			*/
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			/*
			*
			*/
			e.printStackTrace();
		} catch (IOException e) {
			/*
			*
			*/
			e.printStackTrace();
		} catch (Exception e) {
			/* TODO 自动生成 catch 块
			 *
			 */
			e.printStackTrace();
		}

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -