开发者

Inserting XML block into existing XML file in Java

I'm a bit of a newbie to XML. I have a class that creates an xml file (using Jaxb). So I ha开发者_如何学Gove an existing xml file which contains the following.

<bookList>
  <book>
    <title> Grapes of Wrath </title>
    <author> John Steinbeck </author>
  </book>
  <book>
    <title> Harry Potter </title>
    <author> J.K. Rowling </author>
  </book>
<bookList>

In my Java code, I can create a new Book object no problem. But how so I add a new book with all the attributes inside the bookList block? Can I do that in Jaxb or do I need to use something else?


I guess you have a BooksList object generated by JAXB, that BookList object probably has a List of Book. Just add a new book to that list, and marshal the BooksList to get the XML.

Edit: Adding a link to an example that does unmarshalling and marhsalling: http://download.oracle.com/javaee/5/tutorial/doc/bnbay.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜