I am using com.lowagie.text.xml.SAXiTextHandler to convert XML to PDF using iText(v – 2.1.7). I get the below error although i can see that i am using the tags correctly. Verified that the XML formed is valid and i can view it in browser w/o errors. Code fails when trying to do the a parser.parse(….). I have checked the way i have written the tags and it looks fine to me. I have basically added table tags, containing row tags which inturn contains cell tags.
Cell tags have a single paragraph tag inside it (or) sometimes a paragraph and a chunk tag within the paragraph. Is there any way I can get to where this issue happens?
* Removed all the paragraph tags from the XML and tried again. It is returning the same error – Here is the new XML used – http://pastebin.com/hmyPcGDw *
* Even this simple xml file is generating the same exception now – http://pastebin.com/89U9vybt *
To me it looks like an issue with com.lowagie.text.xml.SAXiTextHandler. Any help ?
java.lang.ClassCastException: com.lowagie.text.Paragraph cannot be cast to com.lowagie.text.Table
at com.lowagie.text.xml.SAXiTextHandler.handleEndingTags(Unknown Source)
at com.lowagie.text.xml.SAXiTextHandler.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:606)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endNamespaceScope(XMLDTDValidator.java:2054)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleEndElement(XMLDTDValidator.java:2005)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(XMLDTDValidator.java:879)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1741)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2898)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:302)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:195)
Thats because you have some white space before and after your xml tags (more precisely the white spaces after the
<table>tag). I tried with your example and i had the same exception. I made a copy/paste of your xml code into eclipse (or notepad++) i formatted it (that removes all the unwanted white space) and then i tried again and the problem was solved! the pdf is well generated!