At first time,the xml is not existed.I add the the first records in this xml file while the file is created: It appears like:
but when i add the second record: it appears like:
Can anyone explain me how to cope with the duplicated "<?xml version="1.0" encoding="UTF-8"?>"??? Thanks in advance.... [ January 11, 2004: Message edited by: Mellihoney Michael ]
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.
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.