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

时间:2022-10-25 14:05:12

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

我在现有的xml中添加新节点时遇到了麻烦。所以我有这个代码来创建第一个元素

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>

2 个解决方案

#1


0  

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.

#2


0  

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

要将新书添加到现有文件,首先必须解组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:

在此之后,您必须将新书添加到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 );

#1


0  

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.

#2


0  

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

要将新书添加到现有文件,首先必须解组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:

在此之后,您必须将新书添加到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 );