Can anyone explain me how to cope with the duplicated "<?xml version="1.0" encoding="UTF-8"?>"??? You've got more problems than just that. A valid XML document can have only one "root" element (in your case <booklist> . You really need to rethink how you do this. A simple "append" to a file of serialized XML is never a valid operation. Essentially it seems you have three choices:
to do your "append", read in the existing file to a DOM, add your new node to the document, and re-serialize the whole thing out to the file.
store your uploaded information in some simpler structure (a database, a CSV file, in memory etc.) where an "append" is simpler, and regenerate the XML file from this intermediate structure each time anything is added
store your uploaded information in some simpler structure (a database, a CSV file, in memory etc.) where an "append" is simpler, and generate the XML file only when it is asked for
Which of these options you choose depends on how many times you think the XML file is likely to be read compared to the number of times it will be written, and how many items you think it is likely to contain in total.
Frank,please help: I choose the first method (READ IN DOM and re-serialize). I have changed the servlet as the following:
but when I add new records, it always overwrites the previous one,why?
WHEN I ADD A NEW RECORD:
IT TURNS OUT:
INSTEAD OF :
And I couldn't find where is going wrong... could u help me?
[ January 11, 2004: Message edited by: Mellihoney Michael ]
Joined: Jan 07, 1999
Hmm. It's difficult to tell. You have a lot of code there, some of it duplicated, and most of it grouped into pretty big methods with side effects - all of which makes for hard-to-test code. On the other hand, you seem to have plenty of diagnostic printouts - what did they produce? What DOM is being loaded when you read the existing flle? What DOM is being created when you append your new nodes? I am worried that you have put a gag in the mouth of your system by silently swallowing exceptions, though. An empty catch-block is never a good idea, and one that catches Exception is particularly awful. You simply lose the information if your program is throwing an exception or not. Please put an e.printStackTrace() in your catch block, and let us know what it says.
Joined: Nov 27, 2002
I listed methods which may cause potential errors: