XML
  Home arrow XML arrow Page 2 - Java and XML Basics, Part 3
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Java and XML Basics, Part 3
By: Liviu Tudor
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 22
    2004-04-20

    Table of Contents:
  • Java and XML Basics, Part 3
  • Which One is the Better One to Use?
  • Running the Parser
  • Problems with Big XML Files
  • Validating Parsers - DOM
  • Where do We Get a Validating Parser?
  • ErrorHandler
  • Validating Parsers - SAX
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Java and XML Basics, Part 3 - Which One is the Better One to Use?


    (Page 2 of 9 )

    The latter question though is not that easy to answer; while in most cases choosing in between SAX and DOM proves to be a matter of tastes with some programmers, we are going to look at some speed and performance comparisons in between the two of them. While there are complex ways to evaluate the performance of a computer program, we are only going to refer to two main factors:

    • Memory consumption
    • Execution speed

    For this we have modified the SimpleDOMParser3.java and SimpleSAXParser6.java files and added some lines to measure the execution time and the memory usage as it can be seen:

    SimpleDOMParser4.java


    /**
     * Parse the document now
     */

    System
    .gc();
    long tmStart 
    System.currentTimeMillis();
    long memStart 
    Runtime.getRuntime().freeMemory();
    try
    {
     doc 
    builder.parseisXML );
    }
    catch
    IOException ioe )
    {
     System
    .err.println"I/O error while reading from file:" );
     ioe
    .printStackTrace();
     System
    .exit( );
    }
    catch
    org.xml.sax.SAXException saxe )
    {
     System
    .err.println"Parsing error:" );
     saxe
    .printStackTrace();
     
    System.exit( );
    }
    System
    .gc();
    long tmEndParse 
    System.currentTimeMillis();
    long memEnd 
    Runtime.getRuntime().freeMemory();
    System
    .out.println"Parsing took : " + (tmEndParse tmStart) + " msec" );
    System
    .out.println"Memory occupied : " + (memStart memEnd) + " bytes" );

    SimpleSAXParser7.java


    /**
     * Parse the document now
     */

    System
    .gc();
    long tmStart 
    System.currentTimeMillis();
    long memStart 
    Runtime.getRuntime().freeMemory();
    try
    {
     sax
    .parseisXML, new SimpleSAXParser7() );
    }
    catch
    IOException ioe )
    {
     System
    .err.println"I/O error while reading from file:" );
     ioe
    .printStackTrace();
     System
    .exit( );
    }
    catch
    org.xml.sax.SAXException saxe )
    {
     System
    .err.println"Parsing error:" );
     saxe
    .printStackTrace();
     System
    .exit( );
    }
    System
    .out.println();
    System
    .gc();
    long tmEnd 
    System.currentTimeMillis();
    long memEnd 
    Runtime.getRuntime().freeMemory();
    System
    .out.println"Parsing took " + (tmEnd tmStart) + " msec" );
    System
    .out.println"Memory occupied " + (memStart memEnd) + " bytes" );

    As you can see we don't do any fancy time estimation, just base everything on the computer clock and measure the number of milliseconds it takes for the process to finish -- of course in this process we don't just execute parsing code but a few other things as well (check for exceptions etc.), but we consider the time spent on executing these instructions as irrelevant -- and even if it were, nearly the same code appears in both files!

    Also, to have an estimate of the memory occupied, we just compare the available free memory (in bytes) before and after the parsing. Again, in this process memory can be occupied not just by the variables and structures involved in parsing but by Java internal structures too (buffers, stacks, unclaimed memory blocks etc.). To make the result look closer to reality, we are suggesting to the interpreter that the garbage collection should be kicked off before and right after the parsing -- this will give a more accurate figure of the memory available. Of course, there is no way to enforce garbaged memory recollection, however, as we are running in a single-user single-threaded non-stressful environment, we can assume that in most cases garbage collection will take place when we have queued up the request.

    More XML Articles
    More By Liviu Tudor


     

    XML ARTICLES

    - Using Regions with XSL Formatting Objects
    - Using XSL Formatting Objects
    - More Schematron Features
    - Schematron Patterns and Validation
    - Using Schematron
    - Datatypes and More in RELAX NG
    - Providing Options in RELAX NG
    - An Introduction to RELAX NG
    - Path, Predicates, and XQuery
    - Using Predicates with XQuery
    - Navigating Input Documents Using Paths
    - XML Basics
    - Introduction to XPath
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek