把下面的xml文件变成java类xml version=10catalog book id=bk101 authorGambardella Matthewauthor titleXML Developers Guidetitle genreComputergenre price4495price publish_date2000-10-01pu
下面是将XML文件转换为Java类的示例代码:
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "catalog")
public class Catalog {
private List<Book> books;
@XmlElement(name = "book")
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "book")
public class Book {
private String id;
private String author;
private String title;
private String genre;
private double price;
private String publishDate;
private String description;
@XmlAttribute(name = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(name = "author")
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@XmlElement(name = "title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@XmlElement(name = "genre")
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
@XmlElement(name = "price")
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@XmlElement(name = "publish_date")
public String getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate = publishDate;
}
@XmlElement(name = "description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
你可以使用Java的XML绑定库(如JAXB)将XML文件与这些Java类进行转换
原文地址: http://www.cveoy.top/t/topic/iz0l 著作权归作者所有。请勿转载和采集!