943,682 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 6615
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 25th, 2009
0

Re: Delete last line from text file

While you do not need to use the DOM API to handle the XML, it is recommened. Just going through and deleting the last line and appending what you think is right can result in poorly formed XML documents.

If you are having troubles understanding the DOM API please feel free to post your code and ask questions, there are plenty of us here to help.

The basic idea of working with DOM is to:

Get your Document
Load your XML into it
Manipulate/create the tree
Save the document

Adding / removing nodes is very easy and you can build a tree as deep as you need.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Dec 16th, 2009
0

Solution to your problem

You don't need to use DOM API for something as simple as this. Here is the code that you can use directly. I've implemented using RandomAccessfile.
Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. public class PlayerAdder
  3. {
  4. public static void main(String args[]) {
  5. String playerName = "John" ;
  6. String pointCount = "10";
  7. String minutes = "30";
  8. try{
  9. RandomAccessFile rand = new RandomAccessFile("playerData.xml","rw");
  10. long OldLength = rand.length();
  11. long NewLength = OldLength - 20;
  12. rand.setLength(NewLength);
  13. rand.seek(rand.length());
  14. ///adding tags and their data
  15. rand.writeBytes("\n");
  16. rand.writeBytes("\t");
  17. rand.writeBytes("<player>");
  18. rand.writeBytes("\n");
  19. rand.writeBytes("\t");
  20. rand.writeBytes("\t");
  21. rand.writeBytes("<playerName>");
  22. rand.writeBytes(playerName);
  23. rand.writeBytes("</playerName>");
  24. rand.writeBytes("\n");
  25. rand.writeBytes("\t");
  26. rand.writeBytes("\t");
  27. rand.writeBytes("<pointCount>");
  28. rand.writeBytes(pointCount);
  29. rand.writeBytes("</pointCount>");
  30. rand.writeBytes("\n");
  31. rand.writeBytes("\t");
  32. rand.writeBytes("\t");
  33. rand.writeBytes("<minutes>");
  34. rand.writeBytes(minutes);
  35. rand.writeBytes("</minutes>");
  36. rand.writeBytes("\n");
  37. rand.writeBytes("\t");
  38. rand.writeBytes("</player>");
  39. rand.writeBytes("\n");
  40. rand.writeBytes("</indianaTuxPlayer>");
  41. rand.close();
  42. }
  43. catch(Exception e)
  44. {
  45. System.out.println("Exception occured :"+e);
  46. }
  47. }
  48. }
Reputation Points: 10
Solved Threads: 2
Newbie Poster
camocspro is offline Offline
4 posts
since Dec 2009
Dec 16th, 2009
0
Re: Delete last line from text file
:sigh: double :sigh:
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: CONVERT
Next Thread in Java Forum Timeline: Asmin privileges..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC