xml,java

很多应用代码,对于xml只是一个解析工作而已,不能马上绑定到java 对象。对于对象,每次都需要set 或者get相应的属性,当然也可以使用map 来保存xml配置。于是,一种新的处理方式用于对象和xml之间的映射就变得非常需要,还好sun提供了jaxb,一种很方便的方式来处理java对象和xml内容
OXM
(Object XML Mapping),JAXB原来为JavaEE的一部分,在JDK1.6中,Sun把JAXB放到了Java SE中,用JDK1.5中
的新特性Annotation来标识要绑定的类和属性等
 
package com.future;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
public class JAXBTest {
 /*
  * 1、marshal java object----document 2、gegXMLFromObject 3、unmarshal
  * document-----java object
  */
 public static void main(String[] args) {
  try {
   JAXBContext context = JAXBContext.newInstance(Person.class);
   //gegXMLFromObject(context);
   getObjectFromXML(context);
  } catch (JAXBException e) {
   e.printStackTrace();
  }
 }
 public static void gegXMLFromObject(JAXBContext context) {
  try {
   // 下面代码演示将对象转变为xml Object------XML
   Address address = new Address("China", "tianjin", "tanggu",
     "天津科技大学", "100871");
   Person p = new Person(Calendar.getInstance(), "zhansan", address,
     Gender.MALE, "professor");
   // 指定对应的xml文件
   FileOutputStream fw = new FileOutputStream("c:/person.xml");
   Marshaller m = context.createMarshaller();
   // 将对象转换为对应的XML文件
   m.marshal(p, fw);
  } catch (JAXBException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 public static void getObjectFromXML(JAXBContext context) {
  try {
   // 下面代码演示将上面生成的xml转换为对象 XML------Object
   FileReader fr = new FileReader("c:/person.xml");
   Unmarshaller um = context.createUnmarshaller();
   Person p2 = (Person) um.unmarshal(fr);
   System.out.println("Country:" + p2.getAddress().getCountry());
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (JAXBException e) {
   e.printStackTrace();
  }
 }
}
@XmlRootElement
// 表示person是一个根元素
class Person {
 @XmlAttribute
 String name;
 @XmlElement
 Calendar birthDay;
 @XmlElement
 Address address;
 @XmlElement
 Gender gender;
 @XmlElement
 String job;
 public Address getAddress() {
  return address;
 }
 public Person() {
 }
 public Person(Calendar birthDay, String name, Address address,
   Gender gender, String job) {
  this.birthDay = birthDay;
  this.name = name;
  this.address = address;
  this.gender = gender;
  this.job = job;
 }
}
enum Gender {
 MALE("male"), FEMALE("female");
 private String name = null;
 private Gender(String name) {
  this.name = name;
 }
 @Override
 public String toString() {
  return name;
 }
}
class Address {
 @XmlAttribute
 String country;
 @XmlElement
 String state;
 @XmlElement
 String city;
 @XmlElement
 String street;
 String zipcode;
 public Address() {
 }
 public Address(String country, String state, String city, String street,
   String zipcode) {
  this.country = country;
  this.state = state;
  this.city = city;
  this.street = street;
  this.zipcode = zipcode;
 }
 public String getCountry() {
  return country;
 }
}

猜你喜欢

转载自zengshaotao.iteye.com/blog/1725837