Search |
|||
Joshua Marinacci's blogXML Utility LibraryPosted by joshy on June 28, 2010 at 8:15 PM PDT
As part of some open source stuff I've been doing on the side I've had to generate and parse a lot of XML. I like working with the DOM because it's tree structure cleanly matches my needs, but the W3C API is *so* cumbersome. The DOM was designed to be implemented in any language, not just clean OO languages like Java, so any code using it will work but be ugly. After considering a few other XML libraries I decided to write a new one that would work with modern Java 5 language features like generics, enhanced for-each, and varargs. This library is super tiny because it simply wraps the standard javax.xml libraries in the JRE, but gives you a much nicer interface to work with. Here's how to use it (or download it here): Generating XMLThe
XMLWriter also uses method chaining to let you start and end an element on the same line. Here is a complete XML of generating XLM to the foo.xml file with a standard XML header, var args, and method chaining:
produces:
Parsing XMLThe XMLParser class uses a DOM Parser and XPath to extract the parts of the document you want. Combined with generics and iterators you can conveniently parse your XML in a loop. For example, to parse the document from the previous example back in, grabbing all of the bar elements, then print out their id attributes:
DetailsThis XML library uses the standard W3C Dom and javax.xml parsers underneath. Each DOM element is wrapped by a custom class with the convenience methods. Only elements returned from an XPath query are wrapped, so if you skip most of the document then most of it will never get wrapped. The Doc and Elem objects have references to the underlying W3C DOM objects. I have no real plans for this library. I just found it useful for me and thought you might be interested in it. I'll release new versions as I fix bugs and add (tiny) features. Docs and download here »
Comments
Comments are listed in date ascending order (oldest first)
attributes
Submitted by tbee on Tue, 2010-06-29 00:56.
For readability I would go for chaining add attribute methods: out.start("foo").attr("version","0.01").attr("type","bar")
Hi. I think the current
Submitted by kdt on Tue, 2010-06-29 23:20.
Hi. I think the current vararg params are quite readable
with line separators:
out.start(
And why not to use Object instead of String? It's less typesafe but a little bit more readable ;-) out.start("bar","id",i).end(); Fluid API
Submitted by philho on Tue, 2010-06-29 05:41.
I agree with tbee, "fluid API" is a nice way to get things done while keeping readability without being verbose. Except that I would use out.tag("foo").attr("version", "0.01").attr("type", "bar").end();
Actually, I see in the (terse!) JavaDoc that there is already an attr() method, except it is void, so it doesn't allow chaining calls. Also I see no way to add textual data (Value).
Last remark: the library is BSD but I see no way to get the source code... :-)
|
Recent EntriesCategoriesArchivesJune 2010
April 2010 February 2010 January 2010 October 2009 June 2009 May 2009 March 2009 February 2009 December 2008 November 2008 August 2008 July 2008 June 2008 May 2008 April 2008 March 2008 February 2008 January 2008 December 2007 November 2007 October 2007 September 2007 August 2007 July 2007 June 2007 May 2007 April 2007 March 2007 February 2007 January 2007 December 2006 November 2006 October 2006 September 2006 August 2006 July 2006 June 2006 May 2006 April 2006 March 2006 February 2006 November 2005 September 2005 August 2005 July 2005 June 2005 May 2005 April 2005 March 2005 February 2005 January 2005 December 2004 October 2004 September 2004 August 2004 July 2004 June 2004 May 2004 April 2004 March 2004 February 2004 January 2004 December 2003 November 2003 October 2003 September 2003 August 2003 |
||
|
Download Links