advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the code for this article
Build a Lightweight XML DOM Parser with C#
Can you think of ways to make this parser more efficient? Do you have general parsing tips or comments?
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 3.8/5 | Rate this item | 9 users have rated this item.
 

Build Your Own Lightweight XML DOM Parser

Most currently available Java XML parsers are rich in functionality, but in some cases these full-featured parsers are too large for resource-limited environments, such as Java applets or J2ME applications. Don't count XML out yet though; you can write your own lightweight Java XML parser in fewer than 400 lines of code! 


advertisement
Editor's Note: This tutorial is also available for Visual Basic developers. Get the VB version.

ML is rapidly gaining popularity with application developers as a data storage and exchange format because of its readability, ease of use, and firewall friendliness. The full-featured Java XML parser implementations currently available are large and powerful—and consume resources commensurate with that power at run time. For example, the popular Apache Xerces-J parser is over 1.7 MB, while the latest complete Sun JAXP (Java API for XML Processing) implementation package is more than 3 MB.



These XML parsers offer rich functionality; they support XML namespaces, DTD or schema validation, multiple character-set encodings, etc.; however, not every real-world project requires all that functionality. An application may need only a simple data transfer protocol within a closed environment, or may work with XML data that's always valid and uses only simple ASCII characters. Under those circumstances, employing a full-blown XML parser is probably overkill. Furthermore, if the deploying environment is a Java applet or a J2ME application, the network bandwidth or system memory constraints may make using a full-featured XML parser unpalatable.

Enter the SimpleDOMParser
The SimpleDOMParser is a highly simplified and ultra-lightweight XML DOM parser written in Java. You can deploy the entire parser as a .jar file in less than 4 KB. The source code is fewer than 400 lines long.

Obviously, with such a small code base, the SimpleDOMParser won't support XML namespaces, understand multiple character-set encoding, or validate documents against a DTD or schema; but what the SimpleDOMParser can do well is parse a stream of well-formed XML tags into a DOM-like element tree, letting you perform the common task of extracting data from XML-formatted text.

Why use DOM as a model rather than SAX? The DOM provides an easier-to-use programming interface than SAX. Unlike SAX, when you process an XML document as a DOM tree, all the information within the document is always available. Although the SAX parsing model provides better performance and far less memory usage than the DOM model, most developers have, at times, found themselves building a complete or partial DOM tree anyway when using SAX. Using SAX, an application processes only one tag at a time. If other tags' contents have to be utilized during the processing, you must maintain some sort of global state throughout the process. Maintaining that global state is essentially the purpose of the DOM model. But many small XML applications don't need the full DOM model. Therefore, the SimpleDOMParser provides access to tag names, hierarchy, and content but doesn't bother with many features found in the full W3C DOM specification.

  Next Page: Simplifying the DOM Model


Page 1: IntroductionPage 3: Defining XML Parsing Primitives
Page 2: Simplifying the DOM ModelPage 4: XML Parsing Strategy and SimpleDOMParser Implementation
Please rate this item (5=best)
 1  2  3  4  5
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs