Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can anyone help?I still can't append records in xml...

 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the servlet class:


the bean class:

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 ]
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Mellihoney Michael
    Ranch Hand
    Posts: 124
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks
    Frank
     
    Mellihoney Michael
    Ranch Hand
    Posts: 124
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ]
     
    Frank Carver
    Sheriff
    Posts: 6920
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Mellihoney Michael
    Ranch Hand
    Posts: 124
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I listed methods which may cause potential errors:


     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic