我有一个工作示例:
1)XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bookstore>
<bookList>
<book>
<author>Neil Strauss</author>
<title>The Game</title>
<publisher>Harpercollins</publisher>
<isbn>978-0060554736</isbn>
</book>
<book>
<author>Charlotte Roche</author>
<title>Feuchtgebiete</title>
<publisher>Dumont Buchverlag</publisher>
<isbn>978-3832180577</isbn>
</book>
</bookList>
<location>Frankfurt Airport</location>
<name>Fraport Bookstore</name>
</bookstore>
2)Java类:
书店
package jaxbtest;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="bookstore") // (namespace = "de.vogella.xml.jaxb.model", name="bookstore")
public class Bookstore {
// XmLElementWrapper generates a wrapper element around XML representation
@XmlElementWrapper(name = "bookList")
// XmlElement sets the name of the entities
@XmlElement(name = "book")
private ArrayList<Book> bookList;
private String name;
private String location;
public void setBookList(ArrayList<Book> bookList) {
this.bookList = bookList;
}
public ArrayList<Book> getBooksList() {
return bookList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
3) 书
package jaxbtest;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "book")
// If you want you can define the order in which the fields are written
// Optional
@XmlType(propOrder = { "author", "name", "publisher", "isbn" })
public class Book {
private String name;
private String author;
private String publisher;
private String isbn;
// If you like the variable name, e.g. "name", you can easily change this
// name for your XML-Output:
@XmlElement(name = "title")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
}
4) 主要
package jaxbtest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class BookMain {
private static final String BOOKSTORE_XML = "./bookstore-jaxb.xml";
public static void main(String[] args) throws JAXBException, IOException {
ArrayList<Book> bookList = new ArrayList<Book>();
// create books
Book book1 = new Book();
book1.setIsbn("978-0060554736");
book1.setName("The Game");
book1.setAuthor("Neil Strauss");
book1.setPublisher("Harpercollins");
bookList.add(book1);
Book book2 = new Book();
book2.setIsbn("978-3832180577");
book2.setName("Feuchtgebiete");
book2.setAuthor("Charlotte Roche");
book2.setPublisher("Dumont Buchverlag");
bookList.add(book2);
// create bookstore, assigning book
Bookstore bookstore = new Bookstore();
bookstore.setName("Fraport Bookstore");
bookstore.setLocation("Frankfurt Airport");
bookstore.setBookList(bookList);
// create JAXB context and instantiate marshaller
JAXBContext context = JAXBContext.newInstance(Bookstore.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// Write to System.out
m.marshal(bookstore, System.out);
// Write to File
m.marshal(bookstore, new File(BOOKSTORE_XML));
// get variables from our xml file, created before
System.out.println();
System.out.println("Output from our XML File: ");
Unmarshaller um = context.createUnmarshaller();
Bookstore bookstore2 = (Bookstore) um.unmarshal(new FileReader(BOOKSTORE_XML));
ArrayList<Book> list = bookstore2.getBooksList();
for (Book book : list) {
System.out.println("Book: " + book.getName() + " from "
+ book.getAuthor());
}
/*
String xmlStr = readFileToString(BOOKSTORE_XML);
Bookstore bookstore = getPartial3(Bookstore.class ,xmlStr);
for (Book b : bookstore.getBooksList())
System.out.println(b.getName()+ " ---- " + b.getAuthor() );
xmlStr = readFileToString("./listApplianceOfferings.xml");
ApplianceList bookstore1 = getPartial3(ApplianceList.class ,xmlStr);
for (Appliance b : bookstore1.getBooksList())
System.out.println(b.getName() + " ---- " + b.getDescription() );
*/
//88888
// String xmlStr = readFileToString("./listStorageOfferings.xml");
// StorageOfferings storageOfferingsList = jaxbXmlString2Class(StorageOfferings.class ,xmlStr);
StorageOfferings storageOfferingsList = jaxbXmlFile2Class(StorageOfferings.class ,"./listStorageOfferings.xml");
for (StorageOffering s : storageOfferingsList.getStorageOfferingList() )
System.out.println("8888--> "+ s.getName() + " ---- " + s.getId() );
//99999
}//main
private static void getPartial() {
// get variables from our xml file, created before
System.out.println();
System.out.println("Output from our XML File: ");
Unmarshaller um;
try {
JAXBContext context = JAXBContext.newInstance(Bookstore.class);;
um = context.createUnmarshaller();
// um.setEventHandler(new DefaultValidationEventHandler());
FileReader fileReader = new FileReader(BOOKSTORE_XML);
Bookstore bookstore = (Bookstore) um.unmarshal(fileReader );
ArrayList<Book> list = bookstore.getBooksList();
for (Book book : list) {
System.out.println("Book: " + book.getName() + " from "
+ book.getAuthor());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void getPartial1() {
// get variables from our xml file, created before
System.out.println();
System.out.println("Output from our XML File: ");
Unmarshaller um;
try {
JAXBContext context = JAXBContext.newInstance(ApplianceList.class);;
um = context.createUnmarshaller();
// um.setEventHandler(new DefaultValidationEventHandler());
FileReader fileReader = new FileReader("./listApplianceOfferings.xml");
ApplianceList alist = (ApplianceList) um.unmarshal(fileReader );
ArrayList<Appliance> list = alist.getBooksList();
for (Appliance book : list) {
System.out.println("appliance: " + book.getName() + " from "
+ book.getDescription());
}
} catch (Exception e) {
e.printStackTrace();
}
}
//-------------------
private static <T> T getPartial2(Class<T> cls, String fname) {
// get variables from our xml file, created before
System.out.println();
System.out.println("Output from our XML File: ");
Unmarshaller um;
T alist = null;
try {
JAXBContext context = JAXBContext.newInstance(cls);;
um = context.createUnmarshaller();
// um.setEventHandler(new DefaultValidationEventHandler());
FileReader fileReader = new FileReader(fname);
alist = (T) um.unmarshal(fileReader );
//ArrayList<?> list = alist.getBooksList();
// for (Appliance book : list) {
// System.out.println("appliance: " + book.getName() + " from "
// + book.getDescription());
// }
} catch (Exception e) {
e.printStackTrace();
}
return alist;
}
//-------------------
private static <T> T jaxbXmlFile2Class(Class<T> cls, String fileName) {
FileReader fr = null;
char [] cbuf = null;
try {
fr = new FileReader(fileName);
cbuf = new char[3000];
Arrays.fill(cbuf, ' ');
fr.read(cbuf);
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
try { fr.close(); }
catch (IOException e) {
e.printStackTrace();
}
}
String s = new String(cbuf);
return jaxbXmlString2Class(cls, s);
}
@SuppressWarnings("unchecked")
private static <T> T jaxbXmlString2Class(Class<T> cls, String xmlStr) {
// get variables from our xml file, created before
System.out.println();
System.out.println("Output from our XML File: ");
Unmarshaller um;
T alist = null;
try {
JAXBContext context = JAXBContext.newInstance(cls);;
um = context.createUnmarshaller();
System.out.println(xmlStr);
InputStream is = new ByteArrayInputStream(xmlStr.getBytes());
alist = (T) um.unmarshal( is );
} catch (Exception e) {
e.printStackTrace();
}
return alist;
}
}