生成的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");
同时,进入同一个文件。有什么想法吗?
答案 0 :(得分:0)
你可以包装'预订'带有书籍的元素'标签
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.netbeans.org/schema/books"
xmlns:tns="http://xml.netbeans.org/schema/books"
elementFormDefault="qualified">
<xsd:element name="books">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="book">
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Publisher" type="xsd:string"/>
<xsd:element name="Description" type="xsd:string"/>
<xsd:element name="Price" type="xsd:float"/>
<xsd:element name="PublicationYear" type="xsd:int"/>
<xsd:element name="book">
</xsd:sequence>
<xsd:attribute name="ISBN" type="xsd:int"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>