一、概念____________________________________________________________
注解,作用类似有xml配置文件,避免同一份信息保存在两个地方。
@Override,@Deprecated (key,value)都省略了
@Retention(RetentionPolicy.RUNTIME)只有一个键时,key可以省略
@SupressWarnings({"uncheck", "unused"})大括号表示数组类型,key只有一个,省略
@XmlElement(name = "id")配置参数,键值对
注解用@interface用来声明定义,元注解,在java.lang.annotation包里面,有4个
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Documented {//are to be documented by javadoc and similar tools by default }
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Inherited {// }
//@Retention保留策略: CLASS、RUNTIME和SOURCE,分别表示注解保存在类文件、JVM运行时刻和源代码中。 //只有RUNTIME,才能在运行时刻通过反射API来获取到注解的信息,类似SSH的xml配置文件的注入作用 @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { RetentionPolicy value(); }
//@Target用来声明注解的作用范围,如类型、方法和域等 @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target { ElementType[] value(); }
二、JAXB例子____________________________________________________________
JAXB(Java Architecture for XML Binding)
/** * @author timeriver.wang * @date 2013-01-09 8:07:01 PM */ @XmlRootElement @XmlAccessorType(XmlAccessType.PROPERTY) public class Student { private String id; private String name; public String getId() { return id; } @XmlElement(name = "id") public void setId(String id) { this.id = id; } @XmlElement(name = "name") public String getName() { return name; } public void setName(String name) { this.name = name; } }
/** * @author timeriver.wang * @date 2013-01-09 8:07:09 PM */ @XmlRootElement public class Teacher { private String id; private String name; private List<Student>students; @XmlAttribute (name = "tid") public String getId() { return id; } public void setId( String id ) { this.id = id; } @XmlElement(name = "tname") public String getName() { return name; } public void setName( String name ) { this.name = name; } @XmlElement(name = "students") public List<Student> getStudents() { return students; } public void setStudents( List<Student> students ) { this.students = students; } }
/** * @author timeriver.wang * @date 2013-01-09 8:08:15 PM */ public class Test { public static void main( String[] args )throws Exception { // toXml(); // toStu(); toXmls(); } public static void toXml()throws Exception { Student stu = new Student(); stu.setId( "007" ); stu.setName( "zhouxingxing" ); File file = new File( "D:/stu.xml" ); JAXBContext jaxbContext = JAXBContext.newInstance( Student.class ); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true ); jaxbMarshaller.marshal( stu, file ); jaxbMarshaller.marshal( stu, System.out ); } public static void toStu()throws Exception { File file = new File( "D:/stu.xml" ); JAXBContext jaxbContext = JAXBContext.newInstance( Student.class ); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Student stu = (Student) jaxbUnmarshaller.unmarshal( file ); System.out.println( stu ); } public static void toXmls()throws Exception { Student stu = new Student(); stu.setId( "007" ); stu.setName( "zhouxingxing" ); Student stu2 = new Student(); stu2.setId( "008" ); stu2.setName( "dawenxi" ); List<Student>students = new ArrayList<Student>(); students.add( stu ); students.add( stu2 ); Teacher teacher = new Teacher(); teacher.setId( "101" ); teacher.setName( "DASHI" ); teacher.setStudents( students ); File file = new File( "D:/teacher.xml" ); JAXBContext jaxbContext = JAXBContext.newInstance( Teacher.class ); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true ); jaxbMarshaller.marshal( teacher, file ); jaxbMarshaller.marshal( teacher, System.out ); } }
相关推荐
XML Schema描述了XML文档的结构。可以用一个指定的XML Schema来验证某个XML文档,以 ... 命令介绍本节您将只用到Schema/WSDL视图
JAXB(Java Architecture for XML Binding)是Java平台中用于XML和Java对象之间转换的一个标准技术。这个简易教程将引导我们了解如何使用JAXB进行XML到Java对象以及Java对象到XML的转换,这对于处理XML数据在Java...
解压后进入bin文件夹,双击jadx-gui.bat,进入界面化工具,在工具中打开自己的apk,就可以查看源码,附上官网下载地址:https://github.com/skylot/jadx/releases
该项目是一款基于freeRTOS操作系统和STM32F103x微控制器的手机远程控制浴室温度系统设计源码,共包含1087个文件,包括580个C语言源文件、269个头文件、45个汇编源文件、36个数据文件、36个目标文件、35个编译规则文件、28个包含文件、27个文本文件、6个源文件、3个归档文件。此系统通过手机远程实现对浴室温度的有效控制,适用于智能浴室环境管理。
labview程序代码参考学习使用,希望对你有所帮助。
labview程序代码参考学习使用,希望对你有所帮助。
labview程序代码参考学习使用,希望对你有所帮助。