spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / xml / column11

SAX and DOM and Rock'n Roll

Vice President of Risk Technology - READY TO HIRE! (NYC)
Next Step Systems
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


SAX vs. DOM

The DOM is a quite convenient way to access and manipulate XML data, but it comes with a price to pay:

SAX is more useful in cases where:

Some projects have tried to improve on DOM in this respect. The Java community in particluar has looked closely at ways to tie specific XML elements to specific Java classes. Popular efforts include:

XParse

XParse is an XML-compliant parser written in less than five kilobytes of Javascript. It takes a Javascript string of XML and converts it into a Javascript array representing the object model, one array element per XML element. For more information see Jeremy's Web site. I took the liberty of adapting this fine piece of software to Java. Since the syntax of these two languages is fairly similar, it was not too difficult. In fact, the main challenge lay in mimicking Javascript objects, especially their dynamic behavior for attaching new properties and accessing arrays.

XParse for Java

The translation of XParse from Javascript to Java was as simple as commenting out three lines of unreached return statements in the code. The commented source can be inspected at SourceForge.

JSArray

The Javascript code in XParse makes heavy use of the language's built-in array type JSArray, so the easiest strategy was to write a corresponding Java class that mimics the behavior of the Javascript data structure, at least to the extent needed in this specific case.

Without going into too much detail the key feature of a JSArray is, not surprisingly, the ability to hold a set of objects referenced by indices. Furthermore, it is possible to directly address the contained object's properties by specifying array[index].property. This behavior is approximated by checking for specific object types and property names and then "manually" setting the property on the object. A fully generic implementaion would be feasible through the use of Java's reflection capabilities. Another powerful feature is the ability to split a string into an array using a certain delimiter within the string; similarly, in reverse, joining an array of strings back into one including a potentially different delimiter string. If you are familiar with perl this feature should sound familiar. See the source code with comments if you like.

We conclude with XPath.

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Produced by Michael Claßen
All Rights Reserved. Legal Notices.

URL: http://www.webreference.com/xml/column11/4.html
Created: Apr. 18, 2000
Revised: Apr. 26, 2000