Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:32:19+00:00 2026-06-07T04:32:19+00:00

I have a simple code that gets xml file from given URL: DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(link); that

  • 0

I have a simple code that gets xml file from given URL:

DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(link);

that code returns xml document (org.w3c.dom.Document). I just need to get size of resulting xml document. Is there any elegant way to do it, WITHOUT involving third-party jars?

P.S. size in KB, or MB, not number of nods

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:32:21+00:00Added an answer on June 7, 2026 at 4:32 am

    First naive version: Load the file into a local buffer. Then you know how long is your input. Then parse the XML out of the buffer:

    URL url = new URL("...");
    InputStream in = new BufferedInputStream(url.openStream());
    ByteArrayOutputStream buffer1 = new ByteArrayOutputStream();
    int c = 0;
    while((c = in.read()) >= 0) {
      buffer1.write(c);
    }
    
    System.out.println(String.format("Length in Bytes: %d", 
        buffer1.toByteArray().length));
    
    ByteArrayInputStream buffer2 = new ByteArrayInputStream(buffer1.toByteArray());
    
    Document doc = DocumentBuilderFactory.newInstance()
        .newDocumentBuilder().parse(buffer2);
    

    Drawback is the additional buffer in RAM.

    Second more elegant version: Wrap the input stream with a custom java.io.FilterInputStream counting the bytes streaming through it:

    URL url = new URL("...");
    CountInputStream in = new CountInputStream(url.openStream());
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
    System.out.println(String.format("Bytes: %d", in.getCount()));
    

    Here is the CountInputStream. All read() methods are overwritten to delegate to the super class and count the resulting bytes:

    public class CountInputStream extends FilterInputStream {
    
      private long count = 0L;
    
      public CountInputStream(InputStream in) {
        super(in);
      }
    
      public int read() throws IOException {
        final int c = super.read();
        if(c >= 0) {
          count++;
        }
        return c;
      }
    
      public int read(byte[] b, int off, int len) throws IOException {
        final int bytesRead = super.read(b, off, len);
        if(bytesRead > 0) {
          count += bytesRead;
        }
        return bytesRead;
      }
    
      public int read(byte[] b) throws IOException {
        final int bytesRead = super.read(b);
        if(bytesRead > 0) {
          count += bytesRead;
        }
        return bytesRead;
      }
    
      public long getCount() {
        return count;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'have written a simple code, that gets accelerometer&orientation meter and display some graphics based
I have a simple java code that reads text csv file that contains sentences
I have a simple script of code that reads a PHP file and when
I have a simple assembly code file called exit.s that looks like the following:
basically I want to encapsulate a simple component from code that I already have.
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I am building a PhoneGap/Cordova app and have a simple code that uses http://ricostacruz.com/jquery.transit/
I have a simple jQuery code that runs on a web page that has
I have a simple python curses code that creates a subwindow. However, in the
I have a simple little code fragment that is frustrating me: HashSet<long> groupUIDs =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.