Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: How to retrieve XML CDATA text contents by org.xml.sax.ext.DefaultHandler2?

From: Stanimir Stamenkov <s7an10@--------.--->
To: NULL
Date: 5/4/2009 4:01:00 PM
Thu, 30 Apr 2009 12:02:36 -0400, /RC/:

> For example I have a XML tag
> 
> <script>
> <![CDATA[
> My script is here
> ]]>
> </script>
> 
> I am using org.xml.sax.ext.DefaultHandler2 to parse my XML
> file. How do I retrieve my script contents?

You retrieve it as ordinary text content delivered through 
'characters' events to your ContentHandler.  Whether the text is 
written as CDATA section (or not) in the source is purely a 
syntactic detail which shouldn't bother you.

> Below two methods have no print out at all
> @Override
> public void endCDATA()
> {
>     System.out.println("End of CDATA");
> }
>     
> @Override
> public void startCDATA()
> {
>     System.out.println("Start of CDATA");
> }
> 
> Thank you very much in advance!

You need to set the "lexical-handler" [1] property of the parser 
with the reference to your handler in addition to setting it as a 
'contentHandler':

     XMLReader parser;
     DefaultHandler2 myHandler;
     ...
     parser.setContentHandler(myHandler);
     parser.setProperty("http://xml.org/sax/properties/"
                        + "lexical-handler", myHandler);

[1]  SAX2 Standard Handler and Property IDs 
<http://www.saxproject.org/apidoc/org/xml/sax/package-summary.html>

-- 
Stanimir


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent