XML转Java类:使用JAXB将XML文件映射到Java对象
\u003c?xml version\u003d"1.0"?\u003e \u003ccatalog\u003e \u003cbook id\u003d"bk101"\u003e \u003cauthor\u003eGambardella, Matthew\u003c/author\u003e \u003ctitle\u003eXML Developer's Guide\u003c/title\u003e \u003cgenre\u003eComputer\u003c/genre\u003e \u003cprice\u003e44.95\u003c/price\u003e \u003cpublish_date\u003e2000-10-01\u003c/publish_date\u003e \u003cdescription\u003eAn in-depth look at creating applications with XML.\u003c/description\u003e \u003c/book\u003e \u003cbook id\u003d"bk102"\u003e \u003cauthor\u003eRalls, Kim\u003c/author\u003e \u003ctitle\u003eMidnight Rain\u003c/title\u003e \u003cgenre\u003eFantasy\u003c/genre\u003e \u003cprice\u003e5.95\u003c/price\u003e \u003cpublish_date\u003e2000-12-16\u003c/publish_date\u003e \u003cdescription\u003eA former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.\u003c/description\u003e \u003c/book\u003e \u003cbook id\u003d"bk103"\u003e \u003cauthor\u003eCorets, Eva\u003c/author\u003e \u003ctitle\u003eMaeve Ascendant\u003c/title\u003e \u003cgenre\u003eFantasy\u003c/genre\u003e \u003cprice\u003e5.95\u003c/price\u003e \u003cpublish_date\u003e2000-11-17\u003c/publish_date\u003e \u003cdescription\u003eAfter the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.\u003c/description\u003e \u003c/book\u003e \u003cbook id\u003d"bk104"\u003e \u003cauthor\u003eCorets, Eva\u003c/author\u003e \u003ctitle\u003eOberon's Legacy\u003c/title\u003e \u003cgenre\u003eFantasy\u003c/genre\u003e \u003cprice\u003e5.95\u003c/price\u003e \u003cpublish_date\u003e2001-03-10\u003c/publish_date\u003e \u003cdescription\u003eIn post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant.\u003c/description\u003e \u003c/book\u003e \u003cbook id\u003d"bk105"\u003e \u003cauthor\u003eCorets, Eva\u003c/author\u003e \u003ctitle\u003eThe Sundered Grail\u003c/title\u003e \u003cgenre\u003eFantasy\u003c/genre\u003e \u003cprice\u003e5.95\u003c/price\u003e \u003cpublish_date\u003e2001-09-10\u003c/publish_date\u003e \u003cdescription\u003eThe two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.\u003c/description\u003e \u003c/book\u003e \u003cbook id\u003d"bk106"\u003e \u003cauthor\u003eRandall, Cynthia\u003c/author\u003e \u003ctitle\u003eLover Birds\u003c/title\u003e \u003cgenre\u003eRomance\u003c/genre\u003e \u003cprice\u003e4.95\u003c/price\u003e \u003cpublish_date\u003e2000-09-02\u003c/publish_date\u003e \u003cdescription\u003eWhen Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.\u003c/description\u003e \u003c/book\u003e \u003cbook id\u003d"bk107"\u003e \u003cauthor\u003eThurman, Paula\u003c/author\u003e \u003ctitle\u003eSplish Splash\u003c/title\u003e \u003cgenre\u003eRomance\u003c/genre\u003e \u003cprice\u003e4.95\u003c/price\u003e \u003cpublish_date\u003e2000-11-02\u003c/publish_date\u003e \u003cdescription\u003eA deep sea diver finds true love twenty thousand leagues beneath the sea.\u003c/description\u003e \u003c/book\u003e \u003cbook id\u003d"bk108"\u003e \u003cauthor\u003eKnorr, Stefan\u003c/author\u003e \u003ctitle\u003eCreepy Crawlies\u003c/title\u003e \u003cgenre\u003eHorror\u003c/genre\u003e \u003cprice\u003e4.95\u003c/price\u003e \u003cpublish_date\u003e2000-12-06\u003c/publish_date\u003e \u003cdescription\u003eAn anthology of horror stories about roaches, centipedes, scorpions and other insects.\u003c/description\u003e \u003c/book\u003e \u003cbook id\u003d"bk109"\u003e \u003cauthor\u003eKress, Peter\u003c/author\u003e \u003ctitle\u003eParadox Lost\u003c/title\u003e \u003cgenre\u003eScience Fiction\u003c/genre\u003e \u003cprice\u003e6.95\u003c/price\u003e \u003cpublish_date\u003e2000-11-02\u003c/publish_date\u003e \u003cdescription\u003eAfter an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum.\u003c/description\u003e \u003c/book\u003e \u003cbook id\u003d"bk110"\u003e \u003cauthor\u003eO'Brien, Tim\u003c/author\u003e \u003ctitle\u003eMicrosoft .NET: The Programming Bible\u003c/title\u003e \u003cgenre\u003eComputer\u003c/genre\u003e \u003cprice\u003e36.95\u003c/price\u003e \u003cpublish_date\u003e2000-12-09\u003c/publish_date\u003e \u003cdescription\u003eMicrosoft's .NET initiative is explored in detail in this deep programmer's reference.\u003c/description\u003e \u003c/book\u003e \u003cbook id\u003d"bk111"\u003e \u003cauthor\u003eO'Brien, Tim\u003c/author\u003e \u003ctitle\u003eMSXML3: A Comprehensive Guide\u003c/title\u003e \u003cgenre\u003eComputer\u003c/genre\u003e \u003cprice\u003e36.95\u003c/price\u003e \u003cpublish_date\u003e2000-12-01\u003c/publish_date\u003e \u003cdescription\u003eThe Microsoft MSXML3 parser is covered in detail, with attention to XML DOM interfaces, XSLT processing, SAX and more.\u003c/description\u003e \u003c/book\u003e \u003cbook id\u003d"bk112"\u003e \u003cauthor\u003eGalos, Mike\u003c/author\u003e \u003ctitle\u003eVisual Studio 7: A Comprehensive Guide\u003c/title\u003e \u003cgenre\u003eComputer\u003c/genre\u003e \u003cprice\u003e49.95\u003c/price\u003e \u003cpublish_date\u003e2001-04-16\u003c/publish_date\u003e \u003cdescription\u003eMicrosoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment.\u003c/description\u003e \u003c/book\u003e \u003c/catalog\u003e 内容:下面是将XML文件转换为Java类的示例代码:
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name \u003d \"catalog\")
public class Catalog {
private List\u003cBook\u003e books;
@XmlElement(name \u003d \"book\")
public List\u003cBook\u003e getBooks() {
return books;
}
public void setBooks(List\u003cBook\u003e books) {
this.books \u003d books;
}
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name \u003d \"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 \u003d \"id\")
public String getId() {
return id;
}
public void setId(String id) {
this.id \u003d id;
}
@XmlElement(name \u003d \"author\")
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author \u003d author;
}
@XmlElement(name \u003d \"title\")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title \u003d title;
}
@XmlElement(name \u003d \"genre\")
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre \u003d genre;
}
@XmlElement(name \u003d \"price\")
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price \u003d price;
}
@XmlElement(name \u003d \"publish_date\")
public String getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate \u003d publishDate;
}
@XmlElement(name \u003d \"description\")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description \u003d description;
}
}
你可以使用Java的XML绑定库(如JAXB)将XML文件与这些Java类进行转换。
原文地址: https://www.cveoy.top/t/topic/qg3i 著作权归作者所有。请勿转载和采集!