gpt4 book ai didi

java - 使用 XML 文件和生成的 JAXB 帮助器类将多个对象条目编码到一个文件中

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:33 27 4
gpt4 key购买 nike

以及生成的 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");

同时,进入同一个文件。有什么想法吗?

最佳答案

您可以使用“books”标签包裹“book”元素

<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>

关于java - 使用 XML 文件和生成的 JAXB 帮助器类将多个对象条目编码到一个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48356394/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com