Results 1 to 6 of 6
  1. #1
    Sculpur is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Problem writing to an xml file

    Hello,
    I am currently trying to make use of last.fm's api, with the following code:
    Java Code:
    		public void RetriveSongs(){
    			try
    			{
    				URL addr = new URL("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=" 
                                    + username + "&api_key=cdb1f1d67c2deba8eab843a922da857c&limit=200");
    				URLConnection conn = addr.openConnection();
    				conn.setDoOutput(true);
    				BufferedReader laino = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    				String line;
    				FileOutputStream fout = new FileOutputStream("songlist.xml");
    				PrintStream xml = new PrintStream(fout);
    				while((line = laino.readLine()) != null){
    					xml.println(line);
    				}
    				xml.close();
    				laino.close();
    			}
    			catch (Exception e) {
    			}
    		}
    Username is a string private to the class where I have this function. Basically, last.fm's api answers to this POST request with an xml code. So I use all those streams and the loop to write that xml to a file on my local host, line by line, until I get an empty line - meaning the xml is over. However, under unknown circumstances (I am unsure when it works and when it doesn't), writing to the file will finish before the xml code from the api is over, resulting in a badly constructed xml file that ends abruptly. I compared the faulty file with the xml input I get when I simply use the URL from the above code in my browser, and there is no empty line at the place where the writing stops, and the browser shows the entire xml code just fine. If anyone can see the problem in this function, the help will be appreciated.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Problem writing to an xml file

    If anyone can see the problem in this function
    First, NEVER let exceptions fall through - the least you should do is print the stack trace
    Java Code:
    try{
        //do something
    }catch(Exception e){
       e.printStackTrace();
    }
    Second, the code you posted is not even compilable, and even if the quotes were escaped in the String you pass to construct a URL to make it compilable, a MalformedURLException would be thrown.

  3. #3
    Sculpur is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem writing to an xml file

    Quote Originally Posted by doWhile View Post
    First, NEVER let exceptions fall through - the least you should do is print the stack trace
    Noted and corrected, thank you. Didn't really think about that at the time. However, the code compiles, no exceptions thrown, and the function works correctly to some point. Maybe the problem could come from the repeated use of the same file? First few tries I checked it and it seem to have been overriden completely with the new version, but now I am wondering if there could be a side effect.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Problem writing to an xml file

    However, the code compiles, no exceptions thrown
    Something happened to the String passed to URL - not in your code, but in your first post (perhaps the forum software is misparsing url's within code tags, which magically rectifies itself over time). As for your problem, you mention there are 'unknown circumstances' - have you reproduced the 'unknown circumstances' with updated code that catches exceptions?

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: Problem writing to an xml file

    Since you're not planning on using the stuff coming back from the connection, why not simply stream the bytes directly and avoid the buffering and string conversion?
    It could be something to do with that causing problems.
    So use the InputStream directly, and feed the byte[] to a FileOutputStream.
    Java Code:
    byte[] buffer = new byte[1024]; // pick whatever value seems fine to you
    InputStream is = conn.getInputStream();
    while (is.read(buffer) > -1) {
        // write to the output stream
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Sculpur is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem writing to an xml file

    I fixed the problem, it turned out to be totally unrelated. I am still unsure what exactly was happening, but I have another class with similar function that requests other information the same way. I discovered that in the code I was using to test how the classes and their function works, the two classes' functions start almost simultaneously. As soon as I prevented them from running at the same time (a wild guess), everything went back to normal. Thank you for your time, and sorry for wasting it.
    Last edited by Sculpur; 06-14-2012 at 08:25 PM. Reason: More info that doesn't require a new post

Similar Threads

  1. Problem with writing out a text file
    By jazzermonty in forum New To Java
    Replies: 2
    Last Post: 08-14-2011, 07:28 PM
  2. problem with writing new data into file
    By sara12345 in forum New To Java
    Replies: 2
    Last Post: 04-13-2010, 02:34 AM
  3. writing to text file problem
    By blumdiggity in forum Networking
    Replies: 1
    Last Post: 02-26-2010, 02:43 PM
  4. Writing to file problem
    By PeraPisar in forum Advanced Java
    Replies: 12
    Last Post: 09-16-2009, 11:00 AM
  5. Problem while writing xml file
    By Rajesh Sahu in forum Java Servlet
    Replies: 0
    Last Post: 12-23-2008, 04:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •