Thursday, August 21, 2008

LexEv XMLReader - converts lexical events into markup

It's often a requirement to preserve entity references through to the output (which are usually lost during parsing) or to process the contents of CDATA sections as markup. The Lexical Event XMLReader wraps the standard XMLReader to convert lexical events into markup so that they can be processed. Typical uses are:

  • Converting cdata sections into markup:


    <![CDATA[ &lt;p&gt; a para &lt;p&gt; ]]>

    to:

    <lexev:cdata> <p> a para </p> </lexev:cdata>



  • Preserving entity references:


    hello&mdash;world

    is converted to:

    hello<lexev:entity name="mdash">—</lexev:entity>world


  • Preserving the doctype declaration:


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    is converted to processing instructions:

    <?doctype-public -//W3C//DTD XHTML 1.0 Transitional//EN?>
    <?doctype-system http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd?>


  • Marking up comments:


    <!-- a comment -->

    is converted to:

    <lexev:comment> a comment </lexev:comment>


To use LexEvXMLReader with Saxon:


java -cp saxon9.jar;LexEvXMLReader.jar net.sf.saxon.Transform -x:com.andrewjwelch.lexev.LexEvXMLReader input.xml
stylesheet.xslt


Make sure LexEvXMLReader.jar is on the classpath, and then tell Saxon to use it with the -x switch (copy and paste this line -x:com.andrewjwelch.lexev.LexEvXMLReader)


To use LexEvXMLReader from Java:

XMLReader xmlReader = new LexEvXMLReader();


You can control the following features of LexEv:


  • enable/disable the marking up of entity references

  • enable/disable the marking up of CDATA sections

  • set the default namespace for the CDATA section markup

  • enable/disable the reporting of the DOCTYPE

  • enable/disable the marking up of comments


You can set these through the API (if you are including LexEv in an application), or from the command line using the following system properties:


  • com.andrewjwelch.lexev.inline-entities

  • com.andrewjwelch.lexev.cdata

  • com.andrewjwelch.doctype.cdataNamespace

  • com.andrewjwelch.lexev.doctype

  • com.andrewjwelch.lexev.comments


For example to set a system property from the command line you would use: -Dcom.andrewjwelch.lexev.comments=false


For support, suggestions and licensing, email lexev@andrewjwelch.com

1 comment:

Unknown said...

Thanks for this article. I did not know about Levex and will look into it more. I would love to get your feedback on an xml parser API developed by Xponent. CAX is a caching API for XML. Xponent is considering developing a Java implementation of CAX, which is curently a .NET API and the first beta was released last week. What do you think of it? Details and the download are at http://www.xponentsoftware.com/cax.aspx