2015-11-02 43 views
2

在现有XML中添加新节点时遇到问题。所以我有这样的代码来创建第一个元素如何向现有XML添加新节点

我在这里设定栏

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

在这里,我创建节点

import org.xml.sax.SAXException; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 
import javax.xml.parsers.ParserConfigurationException; 
import java.io.File; 
import java.io.IOException; 

public class TestingTest { 
    public static void main (String[] args) throws InterruptedException, ParserConfigurationException, SAXException, IOException, JAXBException { 
     File file = new File("D:\\books.xml"); 
     Books bks = new Books(); 
     bks.add(new Book( 
       "fantasy", 
       7111, 
       "Tron", 
       "Brawm", 
       15, 
       true 
     )); 

     JAXBContext jaxbContext = JAXBContext.newInstance(Books.class); 
     Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 
     jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     jaxbMarshaller.marshal(bks,file); 

}} 

所以我试着让重写现有的文件和添加新节点但总是只是重写第一个节点

intput

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

我想看到后

<?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> 
+0

这将是有益的补充实际的XML和您想要实现的结果XML。我也看不到你在哪里阅读和解组现有的XML。 – Rhayene

+0

增加了结果,我想达到什么。 – TheOriginalNickname

回答

0

我想在你的代码中使用的文件只写操作。

你需要做的是解开当前的内容,添加书籍条目并将其编组回去。

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
Books books = (Books) jaxbUnmarshaller.unmarshal(file); 
books.add(book); 
jaxbMarshaller.marshal(bks,file); 

Frank。

0

要你的新书添加到您现有的文件,你首先要解编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); 

现在你可以名帅这个修改书籍就像你已经用新的做:

Marshaller marshaller = context.createMarshaller(); 
marshaller.marshal(books, xmlFile);