当前位置:首页 > 开发 > 互联网 > 正文

JAXB 简单Demo

发表于: 2012-05-28   作者:antlove   来源:转载   浏览次数:
摘要: jaxb.JaxbDemo package jaxb; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.xml.bind.JAXBC
jaxb.JaxbDemo
package jaxb;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import pojo.People;

public class JaxbDemo {
	// 编组数据
	public static void marshalData() throws JAXBException, IOException {
		JAXBContext context = JAXBContext.newInstance(People.class);

		Marshaller marshaller = context.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_ENCODING, "gb2312");// 编码格式
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串
		// 是否省略xml头信息(<?xml version="1.0" encoding="gb2312" standalone="yes"?>)
		marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);

		People people = new People();

		people.setLogo("=====");

		people.setId(1);

		people.setName("son");

		People father = new People();

		father.setId(2);

		father.setName("father");

		people.setFather(father);

		FileOutputStream os = new FileOutputStream("People.xml");

		marshaller.marshal(people, os);

		os.close();
	}

	// 解组数据
	public static void unmarshalData() throws JAXBException, FileNotFoundException {
		JAXBContext context = JAXBContext.newInstance(People.class);

		Unmarshaller unmarshaller = context.createUnmarshaller();

		People p = (People) unmarshaller.unmarshal(new FileInputStream("People.xml"));

		System.out.println(p.getId() + " : " + p.getName());
	}
}

pojo.AbstractPeople
package pojo;

public class AbstractPeople {
	private String logo;

	public String getLogo() {
		return logo;
	}

	public void setLogo(String logo) {
		this.logo = logo;
	}
}

pojo.People
package pojo;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "People")
@XmlAccessorType(XmlAccessType.FIELD)
public class People extends AbstractPeople{
	private int id;

	private String name;

	private People father;

	public People() {
	}

	public People(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public People getFather() {
		return father;
	}

	public void setFather(People father) {
		this.father = father;
	}

}

People.xml
<?xml version="1.0" encoding="gb2312" standalone="yes"?>
<People>
    <logo>=====</logo>
    <id>1</id>
    <name>son</name>
    <father>
        <id>2</id>
        <name>father</name>
    </father>
</People>

JAXB 简单Demo

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
1 jaxb
< xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" /> 一、前言。
WebService传递XML文档,当然也可以传递JSON对象。这节我只针对传递XML,那么JAVA绑定成XML,服务端
WebService传递XML文档,当然也可以传递JSON对象。这节我只针对传递XML,那么JAVA绑定成XML,服务端
VideoView简单学习Demo 运行效果图: [img] [/img] 项目主代码: 一 主类、 package com.amaker.vide
刚刚做了一个loadrunner进行负载测试,把步骤截图给大伙看看。一共三个步骤 一创建/编辑脚本 二运行
Citrus Engine是一个的开源flash平台(platform,也可以说是卷轴类)游戏引擎,它基于Starling Framew
使用iphoneSDK官方NSXMLParserDelegate做的简单xml解析,附带详细注释以及使用到的方法的详细解释,
MEF简介及简单的Demo 文章开始之前,首先简单介绍一下什么是MEF,MEF,全称Managed Extensibility Fr
Delphi 中之 VCLZip 控件很不错的说,结合多线程,很容易搞出颇具特色的软件... 之前写过 VCLZip +
FTP服务器最核心的功能就是提供文件的上传、下载服务。在ESFramework Demo -- 文件传送Demo(附源
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号