如何将新节点添加到现有XML

时间:2015-11-02 07:53:20

标签: 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<>();
    }

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

}}

所以我试图重写现有文件并最后添加一个新节点,但总是只重写第一个节点

输入

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

2 个答案:

答案 0 :(得分:0)

我认为在您的代码中,您使用该文件进行只写操作。

您需要做的是解组当前内容,添加图书条目并对其进行编组。

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

弗兰克。

答案 1 :(得分:0)

要将新书添加到现有文件中,首先必须解组XML:

JAXBContext context = JAXBContext.newInstance( Books.class );
 Unmarshaller unmarshaller = context.createUnmarshaller();
 Books books = (Books) unmarshaller.unmarshal( xmlFile );

在此之后,您必须将新书添加到Books中的列表中:

List<Book> bookList = books.getBooks();
bookList.add( newBook );

现在你可以编辑这些经过修改的书籍,就像你已经使用新书一样:

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