`

JAXB(一)入门--概述,简单例子

 
阅读更多

JAXB(Java Architecture for XML Binding) 

主要方法:Marshaller的marshal()方法,

// marshal 整理,编列,元帅的意思

 

/**
 * @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(namespace ="NAMESPACE" )
@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;
    }
    @XmlElementWrapper(name="students")
    @XmlElement(name = "student")
    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 {
    private static String filePath = "D:/teacher.xml";
    public static void main( String[] args )throws Exception {
        toXml();
        toObj();
    }

    public static void toXml()throws Exception {
        // organize Object(to be saved/persisted)
        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( "daoyan" );
        teacher.setStudents( students );
        
        //
        JAXBContext jaxbContext = JAXBContext.newInstance( Teacher.class );
        // marshal 整理,编列,元帅的意思
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        // format, make every element keep a separate line. 
        jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );

        //output --> file
        File file = new File( filePath );
        jaxbMarshaller.marshal( teacher, file );
        //output --> console
        jaxbMarshaller.marshal( teacher, System.out );
    }
    
    public static void toObj()throws Exception {
        File file = new File( filePath );
        JAXBContext jaxbContext = JAXBContext.newInstance( Teacher.class );
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Teacher teacher = (Teacher) jaxbUnmarshaller.unmarshal( file );
        System.out.println( teacher.getName() );
    }

}

 

分享到:
评论

相关推荐

    jaxb-api-2.3.1-API文档-中文版.zip

    赠送jar包:jaxb-api-2.3.1.jar; 赠送原API文档:jaxb-api-2.3.1-javadoc.jar; 赠送源代码:jaxb-api-2.3.1-sources.jar; 赠送Maven依赖信息文件:jaxb-api-2.3.1.pom; 包含翻译后的API文档:jaxb-api-2.3.1-...

    javax.rar(jaxb-impl-2.3.0、jaxb-core-2.3.0、jaxb-api-2.3.0)

    在Java开发环境中,`javax.rar`这一提及实际上与RAR压缩库无关,而是涉及到Java的XML绑定(JAXB)技术。标题中的"jaxb-impl-2.3.0、jaxb-core-2.3.0、jaxb-api-2.3.0"是JAXB的不同组件版本号,它们在处理XML到Java...

    jaxb-runtime-2.3.5-API文档-中英对照版.zip

    赠送jar包:jaxb-runtime-2.3.5.jar; 赠送原API文档:jaxb-runtime-2.3.5-javadoc.jar; 赠送源代码:jaxb-runtime-2.3.5-sources.jar; 赠送Maven依赖信息文件:jaxb-runtime-2.3.5.pom; 包含翻译后的API文档:...

    jaxb-runtime-2.3.5-API文档-中文版.zip

    赠送jar包:jaxb-runtime-2.3.5.jar; 赠送原API文档:jaxb-runtime-2.3.5-javadoc.jar; 赠送源代码:jaxb-runtime-2.3.5-sources.jar; 赠送Maven依赖信息文件:jaxb-runtime-2.3.5.pom; 包含翻译后的API文档:...

    jaxb-api-2.1.jar 和 jaxb-impl-2.1.8.jar

    总的来说,`jaxb-api-2.1.jar` 和 `jaxb-impl-2.1.8.jar` 在Java开发中扮演着重要的角色,它们为XML数据处理提供了强大的工具,使得XML与Java对象之间的转换变得简单易行。然而,随着技术的发展,开发者也应关注新的...

    jaxb-core-2.2.10-b140310.1920-API文档-中文版.zip

    赠送jar包:jaxb-core-2.2.10-b140310.1920.jar; 赠送原API文档:jaxb-core-2.2.10-b140310.1920-javadoc.jar; 赠送源代码:jaxb-core-2.2.10-b140310.1920-sources.jar; 赠送Maven依赖信息文件:jaxb-core-...

    jaxb-impl-2.2.10-b140310.1920-API文档-中文版.zip

    赠送jar包:jaxb-impl-2.2.10-b140310.1920.jar; 赠送原API文档:jaxb-impl-2.2.10-b140310.1920-javadoc.jar; 赠送源代码:jaxb-impl-2.2.10-b140310.1920-sources.jar; 赠送Maven依赖信息文件:jaxb-impl-...

    jaxb-core-2.3.0.1.jar

    jaxb-core-2.3.0.1.jar

    jaxb-core-2.3.0.jar

    有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-core-2.3.0.jar

    jaxb-impl-2.3.0.jar

    有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-impl-2.3.0.jar

    jaxb-api-2.0.jar

    jaxb-api-2.0.jar jaxb-api-2.0.jar

    jackson-module-jaxb-annotations-2.7.8-API文档-中英对照版.zip

    赠送jar包:jackson-module-jaxb-annotations-2.7.8.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.7.8-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.7.8-sources.jar; 赠送Maven依赖...

    jaxb-impl-2.1.jar

    jaxb-impl-2.1.jar

    jaxb-api-2.3.0-API文档-中英对照版.zip

    赠送jar包:jaxb-api-2.3.0.jar; 赠送原API文档:jaxb-api-2.3.0-javadoc.jar; 赠送源代码:jaxb-api-2.3.0-sources.jar; 赠送Maven依赖信息文件:jaxb-api-2.3.0.pom; 包含翻译后的API文档:jaxb-api-2.3.0-...

    jaxb-api-2.2 jaxb-impl

    B, java web 项目要用一个可以正常启动的web项目(当然没用到jaxb的web项目),打印System.out.println(System.getProperty("java.endorsed.dirs"));即可,我的是java web项目,打出的是E:/tomcat6/common/endorsed ...

    jaxb-impl-2.1.13.jar

    jaxb-impl-2.1.13.jar

    jackson-module-jaxb-annotations-2.7.8-API文档-中文版.zip

    赠送jar包:jackson-module-jaxb-annotations-2.7.8.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.7.8-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.7.8-sources.jar; 赠送Maven依赖...

    jackson-module-jaxb-annotations-2.2.3-API文档-中文版.zip

    赠送jar包:jackson-module-jaxb-annotations-2.2.3.jar; 赠送原API文档:jackson-module-jaxb-annotations-2.2.3-javadoc.jar; 赠送源代码:jackson-module-jaxb-annotations-2.2.3-sources.jar; 赠送Maven依赖...

    jaxb-impl-2.1.9.jar&jaxb-api-2.2.6.jar

    JAXB 提供了一种高效、方便的方式来处理XML文档,使得开发人员无需手动编写XML解析和生成代码,极大地提高了开发效率。 "jaxb-impl-2.1.9.jar" 和 "jaxb-api-2.2.6.jar" 是JAXB框架的两个关键组件: 1. **jaxb-api...

    jaxb-api-2.2.2-API文档-中文版.zip

    赠送jar包:jaxb-api-2.2.2.jar; 赠送原API文档:jaxb-api-2.2.2-javadoc.jar; 赠送源代码:jaxb-api-2.2.2-sources.jar; 赠送Maven依赖信息文件:jaxb-api-2.2.2.pom; 包含翻译后的API文档:jaxb-api-2.2.2-...

Global site tag (gtag.js) - Google Analytics