研究了一陣子, 看了很多文件. 都大概需要使用xml schema 來產生 java to xml.
不過今天看到一個文章 Generate an XML Document from an Object Model with JAXB 2裡頭有範例, 可以不用使用 XML schema. 就來試試看摟.
開發兩個 Enity Bean(Person.java, Address.java) , 還有一個測試轉換的 Test Code(PersonTest.java).
1. 在 Person 中, 會包涵 Address. 所以在 Person 定義 @XmlRootElement. 這樣Address 也會自動被轉成xml.
2. Address 作一個設定, 原本應該要產生 zipcode tag, 如果想要換成別的. 必須先定義@XmlAccessorType(XmlAccessType.FIELD), 然後在 zipcode field在定義@XmlElement(name = "zip") 這樣轉換出來的address tag 會包涵zip tag 不會是 zipcode tag.
Person.java
package jaxbtest;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author longtai
*/
@XmlRootElement
public class Person {
private String name;
private String email;
private Address address;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the address
*/
public Address getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(Address address) {
this.address = address;
}
}
Address.java
package jaxbtest;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
/**
*
* @author longtai
*/
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {
private String address;
@XmlElement(name = "zip")
private String zipcode;
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return the zipcode
*/
public String getZipcode() {
return zipcode;
}
/**
* @param zipcode the zipcode to set
*/
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
PersonTest.java
package jaxbtest;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import junit.framework.JUnit4TestAdapter;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
* @author longtai
*/
public class PersonTest {
public PersonTest() {
}
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(PersonTest.class);
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Test
public void testMarshal() throws Exception {
Person p = new Person();
p.setName("longtai");
p.setEmail("longtai.tw@gmail.com");
Address a = new Address();
a.setAddress("台灣新竹");
a.setZipcode("35002");
p.setAddress(a);
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(Person.class);
Marshaller m = context.createMarshaller();
m.marshal(p, writer);
System.out.println(writer);
}
@Test
public void testUnmarshal() throws Exception
{
StringReader reader = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><person><address><address>%u53F0%u7063%u65B0%u7AF9</address><zip>35002</zip></address><email>longtai.tw@gmail.com</email><name>longtai</name></person>");
JAXBContext context = JAXBContext.newInstance(Person.class);
Unmarshaller um = context.createUnmarshaller();
Person p = (Person)um.unmarshal(reader);
System.out.println(p.getName());
System.out.println(p.getAddress().getAddress());
System.out.println(p.getAddress().getZipcode());
}
}
沒有留言:
張貼留言