简体   繁体   中英

How to add new node to existing XML

I have a trouble in adding new node in existing xml. So I have this code for creating first element

Here I setting fields

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

Here I creating node

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

}}

So Im trying to make rewrite existing file and add a new node in the end but always just rewriting the first node

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>

what I want to see after

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

I think in your code you use the file for write only operations.

What you need to do is unmarshall the current content, add the book entry and marshall it back.

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

Frank.

To add your new book to your existing file you first have to unmarshal the XML:

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

After this you have to add the new book to your the list in Books:

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

Now you can marshal this modified Books like you already have done with the new one:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM