Common Exceptions while working with DOM4J Java

Hi All,

Below is the list of few Exceptions which we face usually working with DOM4J. Just wants to share with you all.

Sample XML with inline DTD.

<?xml version=”1.0″?>

<!DOCTYPE dir[
<!ELEMENT dir (employee+)>
<!ELEMENT employee (name,dept,joind)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT dept (#PCDATA)>
<!ELEMENT joind (#PCDATA)>
]>

<dir>
<employee>
<name>ss</name>
<dept>dept04</dept>
<joind>21-aug-2010</joind>
</employee>

<employee>
<name>employee2</name>
<dept>dept04</dept>
<joind>21-aug-2011</joind>
</employee>

</dir>

Nested exception:

org.xml.sax.SAXParseException: The content of element type “employee” must match “(name,dept,joind)”.

at org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1213)

at org.apache.xerces.validators.common.XMLValidator.reportRecoverableXMLError(XMLValidator.java:1851)

Nested exception:

org.xml.sax.SAXParseException: Element type “joind1” must be declared.

at org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1213)

at org.apache.xerces.validators.common.XMLValidator.reportRecoverableXMLError(XMLValidator.java:1807)

at org.apache.xerces.validators.common.XMLValidator.validateElementAndAttributes(XMLValidator.java:3633)

In this case just check the DTD defined for XML, the above exception states that employee element is having a child element which is not been defined in DTD specification.

Now we can start looking into the below possibilities to solve this:

1.  Check if Multiple/Zero occurrence of the valid element as per the DTD is defined properly (in case if you want to allow multiple/zero occurrence then add “+/?” to the element).

2. New Element being added which is not matching the DTD Specification.

—————————————————-

Nested exception:

org.xml.sax.SAXParseException: The constraint is required after the element type “joind” in the element type declaration.

at org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1213)

at org.apache.xerces.framework.XMLDTDScanner.reportFatalXMLError(XMLDTDScanner.java:660)

at org.apache.xerces.framework.XMLDTDScanner.abortMarkup(XMLDTDScanner.java:709)

at org.apache.xerces.framework.XMLDTDScanner.scanElementDecl(XMLDTDScanner.java:1760)

–>For this exception just check the character data format defined. Set it to either CDATA or PCDATA

——————————————–

Nested exception:

org.xml.sax.SAXParseException: Element type “dir” must be declared.

at org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1213)

at org.apache.xerces.validators.common.XMLValidator.reportRecoverableXMLError(XMLValidator.java:1807)

at org.apache.xerces.validators.common.XMLValidator.validateElementAndAttributes(XMLValidator.java:3633)

at org.apache.xerces.validators.common.XMLValidator.callStartElement(XMLValidator.java:1229)

at org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:938)

–> For this exception the possible workaround is to check if DTD has been linked/attached. Missing DTD ref, no DTD definition could cause this exception.

[NOTE] These all errors occurred when you set the SAXReader.setValidation(true) to validate the XML file,  if you don’t have any valid DTD,  please do not set this API, Default value is false.


Thanks
R Vashi

One thought on “Common Exceptions while working with DOM4J Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s