Linconnue55
Linconnue55

Reputation: 187

Recover the length of xml file

I need to recover the length of a xml file. I have changed the code , please , I need help. It's really annoying.

try {

        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */
        URL sourceUrl = new URL("http://sociable.co/wp-content/uploads/2011/04/android-wallpaper.png");
        URLConnection urlConnection = sourceUrl.openConnection();
        urlConnection.connect();
        int file_size = urlConnection.getContentLength();
        Log.v("the size of this file is : ", ""+file_size);

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));

    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

But when I use log.v, nothing is appeared in the logCat. How can I resolve this?

Upvotes: 0

Views: 158

Answers (4)

Tomap
Tomap

Reputation: 638

Maybe try this:

long contentLength = Long.parseLong(httpconn.getHeaderField("Content-Length"));

Upvotes: 0

MarcFasel
MarcFasel

Reputation: 1158

You cannot cast an XMLReader to a File. You get a ClassCastException. You should see that in your log. Christian is right (except that the method is openConnection() and not connect()).

Replace

long filesize = ((File) xr).length();

with

int filesize = sourceUrl.openConnection().getContentLength();

I have just tried it and it works.

Upvotes: 0

Reno
Reno

Reputation: 33792

I think the problem is that you are trying to parse a png file.

edit: See if your file gets downloaded properly

   void downloadXMLFile(String fileUrl){
              URL myFileUrl = null;
              try {

               myFileUrl= new URL(fileUrl);
              } catch (MalformedURLException e) {

               // TODO Auto-generated catch block
               e.printStackTrace();
          }

          try {

               HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
               conn.setDoInput(true);
               conn.connect();
               int length = conn.getContentLength();
               InputStream is = conn.getInputStream();
          } catch (IOException e) {

               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }
}

Upvotes: 0

Cristian
Cristian

Reputation: 200140

I think this line is incorrect: ((File) xr)... what if you do this:

sourceUrl.connect();
long length = sourceUrl.getContentLength();

Upvotes: 1

Related Questions