This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Can anyone help?I still can't append records in xml... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can anyone help?I still can Watch "Can anyone help?I still can New topic
Author

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

Mellihoney Michael
Ranch Hand

Joined: Nov 27, 2002
Posts: 124
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 ]

a beginner in java
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
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.


    Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
    Mellihoney Michael
    Ranch Hand

    Joined: Nov 27, 2002
    Posts: 124
    thanks
    Frank
    Mellihoney Michael
    Ranch Hand

    Joined: Nov 27, 2002
    Posts: 124
    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

    Joined: Jan 07, 1999
    Posts: 6920
    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

    Joined: Nov 27, 2002
    Posts: 124
    I listed methods which may cause potential errors:


     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Can anyone help?I still can't append records in xml...
     
    Similar Threads
    Parsing XML file using Xpath in jdk1.4
    help!Urgent,my assignment.....
    a ReadXMLFile Servlet works without my expectation...
    session object = null?
    Generating XML from java