XML
  Home arrow XML arrow Page 3 - 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 - Running the Parser


    (Page 3 of 9 )

    Running SimpleSAXParser7 against our first XML example (simple1.xml) produces the following results:

    java -classpath "%CLASSPATH%;." SimpleSAXParser7 simple1.xml
    Parsing took 50 msec
    Memory occupied 1496 bytes
    Parsing successful!

    Running SimpleDOMParser4 against same file produces:

    java -classpath "%CLASSPATH%;." SimpleDOMParser4 simple1.xml
    Parsing took : 50 msec
    Memory occupied : 5680 bytes
    Parsing successful!
    Traversing the DOM took 10 msec
    Total processing time 60 msec

    So over all a delay of about 10 msec (remember that the figures are approximate), and a memory consumption which is bigger by about 4kb when using DOM as opposed to SAX. Now, that isn't too much, most of you will agree (ok, apart from those of you that can are still fanatic about the 48k that the good ole' Sinclair Spectrum used to have), but let's put this in perspective: the simple1.xml file is just slightly over 0.5 Kb, so what would happen if we were to process a large XML file? To test this, I've copied and pasted the data in simple1.xml file quite a few times and ran the tests against this new file (simple4.xml which is now about 65kb worth of XML). The results speak for themselves:

    java -classpath "%CLASSPATH%;." SimpleSAXParser7 simple4.xml

    Parsing took 191 msec
    Memory occupied 1496 bytes
    Parsing successful!

    java -classpath "%CLASSPATH%;." SimpleDOMParser4 simple4.xml
    Parsing took : 300 msec
    Memory occupied : 438856 bytes
    Parsing successful!
    Traversing the DOM took 681 msec
    Total processing time 991 msec

    Now this is significant! While the SAX approach will only take less then 0.2 seconds, the DOM implementation goes to nearly a whole second; also, the memory taken by the DOM approach is huge compared to the (nearly) 1.5Kb in the case of SAX!

    The explanation for this is quite simple:

    1. The DOM parser will have to build a document tree as the parsing goes on -- this takes both time and memory; while in the case of SAX we process the data as it "arrives", without necessary storing it.

    2. Once the document tree is created (in the case of DOM) we then have to step through this tree and find/retrieve the relevant information for us -- in other way we actually step through the DOM tree twice every parsing!

    Assuming that we only make usage of one single item in the whole XML document, the (nearly) 0.5 Mb taken by DOM becomes a waste. However, if we need to come back to the parsed information very often and we are going to make usage of most of the data, then using SAX might be pointless as we will have to gather all this data in a tree/list/stack/array and that might be too much of an overload in terms of programming when it's easier to just use the DOM API to traverse the tree and retrieve individual elements. Also, the DOM API is easier to use from a programmer's point of view, and as the differences are nearly unnoticeable in the case of small files, you could use the DOM approach without too much of an overhead.

    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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek