Java 如何向现有XML添加新节点
在现有xml中添加新节点时遇到问题。所以我有了创建第一个元素的代码 我在这里设置字段Java 如何向现有XML添加新节点,java,xml,marshalling,Java,Xml,Marshalling,在现有xml中添加新节点时遇到问题。所以我有了创建第一个元素的代码 我在这里设置字段 import java.util.*; import javax.xml.bind.annotation.*; @XmlRootElement (name = "books") class Books { private ArrayList<Book> books; public Books(){ books = new ArrayList<>();
import java.util.*;
import javax.xml.bind.annotation.*;
@XmlRootElement (name = "books")
class Books {
private ArrayList<Book> books;
public Books(){
books = new ArrayList<>();
}
public void add(Book book){
books.add(book);
}
@XmlElement(name = "book")
public List<Book> getBooks() {
return books;
}
}
@XmlRootElement(name = "Book")
class Book {
// поля
@XmlAttribute(name="ganre")
String ganre;
@XmlElement
String bookName;
@XmlElement
String bookAuthor;
@XmlElement
int bookId;
@XmlElement
int bookYear;
@XmlElement
boolean bookAvailable;
public Book(){
}
public Book(String ganre, int bookId, String bookName, String bookAuthor, int bookYear, boolean bookAvailable){ // Конструктор чтобы быстрее создавать новые книги не напрягаясь
setGanre(ganre);
setBookId(bookId);
setBookName(bookName);
setBookAuthor(bookAuthor);
setBookYear(bookYear);
setBookAvailable(bookAvailable);
}
String getGanre(){
return this.ganre;
}
void setGanre(String ganre){
this.ganre = ganre;
}
String getBookName(){
return this.bookName;
}
void setBookName(String bookName){
this.bookName = bookName;
}
String getBookAuthor (){
return this.bookAuthor ;
}
void setBookAuthor (String bookAuthor ){
this.bookAuthor = bookAuthor;
}
boolean getBookAvailable(){
return this.bookAvailable;
}
void setBookAvailable(boolean bookAvailable){
this.bookAvailable = bookAvailable;
}
int getBookId(){
return this.bookId;
}
void setBookId(int bookId){
this.bookId = bookId;
}
int getBookYear(){
return this.bookYear;
}
void setBookYear(int bookYear){
this.bookYear = bookYear;
}
}
因此,我试图使重写现有的文件,并在最后添加一个新的节点,但总是只是重写第一个节点
输入
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<books>
<book ganre="fantasy">
<bookName>Tron</bookName>
<bookAuthor>Brawm</bookAuthor>
<bookId>7111</bookId>
<bookYear>15</bookYear>
<bookAvailable>true</bookAvailable>
</book>
特隆
布拉姆
7111
15
真的
我以后想看什么
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<books>
<book ganre="fantasy">
<bookName>Tron</bookName>
<bookAuthor>Brawm</bookAuthor>
<bookId>7111</bookId>
<bookYear>15</bookYear>
<bookAvailable>true</bookAvailable>
</book>
<book ganre="action">
<bookName>Corn</bookName>
<bookAuthor>Down</bookAuthor>
<bookId>312</bookId>
<bookYear>23</bookYear>
<bookAvailable>false</bookAvailable>
</book>
</books>
特隆
布拉姆
7111
15
真的
玉米
向下
312
23
假的
我认为在您的代码中,您将文件用于只写操作
您需要做的是解组当前内容,添加图书条目并将其重新整理
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Books books = (Books) jaxbUnmarshaller.unmarshal(file);
books.add(book);
jaxbMarshaller.marshal(bks,file);
Frank。我认为在您的代码中,您将该文件用于只写操作 您需要做的是解组当前内容,添加图书条目并将其重新整理
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Books books = (Books) jaxbUnmarshaller.unmarshal(file);
books.add(book);
jaxbMarshaller.marshal(bks,file);
Frank.要将新书添加到现有文件中,首先必须解组XML:
JAXBContext context = JAXBContext.newInstance( Books.class );
Unmarshaller unmarshaller = context.createUnmarshaller();
Books books = (Books) unmarshaller.unmarshal( xmlFile );
在此之后,您必须将新书添加到图书列表中:
List<Book> bookList = books.getBooks();
bookList.add( newBook );
要将新书添加到现有文件中,首先必须解组XML:
JAXBContext context = JAXBContext.newInstance( Books.class );
Unmarshaller unmarshaller = context.createUnmarshaller();
Books books = (Books) unmarshaller.unmarshal( xmlFile );
在此之后,您必须将新书添加到图书列表中:
List<Book> bookList = books.getBooks();
bookList.add( newBook );
添加实际的XML和您想要实现的结果XML将非常有用。另外,我看不到您在哪里阅读和解压现有的XML.added结果以及我要实现的内容。添加实际的XML和您想要实现的结果XML会很有用。此外,我看不到您在哪里阅读和解压现有的XML.added结果以及我要实现的目标。