【发布时间】:2018-01-20 12:55:47
【问题描述】:
以及生成的 JAXB 助手类:
package itemOrder;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"title",
"author",
"publisher",
"description",
"price",
"publicationYear"
})
@XmlRootElement(name = "book")
public class Book {
@XmlElement(name = "Title", required = true)
protected String title;
@XmlElement(name = "Author", required = true)
protected String author;
@XmlElement(name = "Publisher", required = true)
protected String publisher;
@XmlElement(name = "Description", required = true)
protected String description;
@XmlElement(name = "Price")
protected float price;
@XmlElement(name = "PublicationYear")
protected int publicationYear;
@XmlAttribute(name = "ISBN")
protected Integer isbn;
/**
* Gets the value of the title property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getTitle() {
return title;
}
/**
* Sets the value of the title property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setTitle(String value) {
this.title = value;
}
/**
* Gets the value of the author property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getAuthor() {
return author;
}
/**
* Sets the value of the author property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setAuthor(String value) {
this.author = value;
}
/**
* Gets the value of the publisher property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getPublisher() {
return publisher;
}
/**
* Sets the value of the publisher property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setPublisher(String value) {
this.publisher = value;
}
/**
* Gets the value of the description property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getDescription() {
return description;
}
/**
* Sets the value of the description property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setDescription(String value) {
this.description = value;
}
/**
* Gets the value of the price property.
*
*/
public float getPrice() {
return price;
}
/**
* Sets the value of the price property.
*
*/
public void setPrice(float value) {
this.price = value;
}
/**
* Gets the value of the publicationYear property.
*
*/
public int getPublicationYear() {
return publicationYear;
}
/**
* Sets the value of the publicationYear property.
*
*/
public void setPublicationYear(int value) {
this.publicationYear = value;
}
/**
* Gets the value of the isbn property.
*
* @return
* possible object is
* {@link Integer }
*
*/
public Integer getISBN() {
return isbn;
}
/**
* Sets the value of the isbn property.
*
* @param value
* allowed object is
* {@link Integer }
*
*/
public void setISBN(Integer value) {
this.isbn = value;
}
}
在 Main 中,使用 XML 模板,我正在为定义的元素设置数据:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
itemOrder.Book quickXML = new itemOrder.Book();
quickXML.setAuthor("Lev Tolstoi");
quickXML.setDescription("Russion fixction about 1st world war");
quickXML.setISBN(62129985);
quickXML.setPrice((float)12.6);
quickXML.setPublisher("Progress");
quickXML.setTitle("War and Peace");
try {
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(quickXML.getClass().getPackage().getName());
javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
OutputStream os = new FileOutputStream("xmlFile.xml");
marshaller.marshal(quickXML, os);
} catch (javax.xml.bind.JAXBException ex) {
// XXXTODO Handle exception
java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
然后将它们编组到一个文件中。问题是,如何将多个数据条目编组到一个文件中。例如:我想编组这个:
quickXML.setAuthor("Lev Tolstoi");
quickXML.setDescription("Russion fixction about 1st world war");
quickXML.setISBN(62129985);
quickXML.setPrice((float)12.6);
quickXML.setPublisher("Progress");
quickXML.setTitle("War and Peace");
还有这个:
quickXML.setAuthor("Robert Schwentke");
quickXML.setDescription("description description description");
quickXML.setISBN(62129432);
quickXML.setPrice((float)10.9);
quickXML.setPublisher("Regress");
quickXML.setTitle("Red");
同时,进入同一个文件。有什么想法吗?
【问题讨论】:
-
所以这不能回答你的问题。但。我在想。你有什么理由不使用 import 语句?例如。在主类的顶部,您可以导入 javax.xml.bind.Marshaller;并在方法本身中输入 Marshaller marshaller = jaxbCtx.createMarshaller(); ...您正在使用 IDE 吗? IDE 可以很好地为您整理这些东西。您不应该在代码中使用完全限定的类名。它使行长一英里,您的代码更难阅读。
-
您可以创建一个包装类并将您的书籍放入书籍列表中:请参阅此处:link