Re: How to retrieve XML CDATA text contents by org.xml.sax.ext.DefaultHandler2?
RC wrote:
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?
Via the 'characters()' method.
What shall I do in these two methods?
Mark the beginning and end of each element so that your parser knows
where it is in the parse process.
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes)
throws SAXException
{
if (qName.equals("script"))
{
// How to retrieve my script contents?
Not here. What do the Javadocs tell you about the purpose of this
method and the event it handles?
}}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
if (qName.equals("script"))
{
// How to retrieve my script contents?
Not here. What do the Javadocs tell you about the purpose of this
method and the event it handles?
}
}
Below two methods have no print out at all
Did you read the Javadocs?
@Override
public void endCDATA()
{
System.out.println("End of CDATA");
}
@Override
public void startCDATA()
{
System.out.println("Start of CDATA");
}
The Javadocs will tell you:
The contents of the CDATA section will be reported through the regular
characters event; this event is intended only to report the boundary.
While not always enough, the API Javadocs are always a good place to
start, and often will completely answer your questions.
--
Lew