Skip to main content

XML document validated but (dom)Document remains empty after parse

No replies
martinmolema
Offline
Joined: 2010-02-24
Points: 0

Hi,

After parsing a valid XML from a string, I get no error, but the xmlDoc variable remains null (If I print it to the System.err). Any suggestions?

I cannot load the .dtd by the way from a file as I'm writing an applet. So the DTD is wrapped in the DOCTYPE in the XML itself.

This is my code:
public class XMLDOMLoader{
public org.w3c.dom.Document xmlDoc;

[..... snip....]

public void readFromString(String xmlRecords)throws XMLDOMLoaderException{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setValidating(doValidate);
docBuilderFactory.setIgnoringComments(true);
docBuilderFactory.setIgnoringElementContentWhitespace(true);

try {
//Get the DocumentBuilder from the factory that we just got above.
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

docBuilder.setErrorHandler(new org.xml.sax.ErrorHandler() {

// ignore fatal errors (an exception is guaranteed)
public void fatalError(SAXParseException exception) throws SAXException {
}

// treat validation errors as fatal
public void error(SAXParseException e) throws SAXParseException {
throw e;
}

// dump warnings too
public void warning(SAXParseException err) throws SAXParseException {
throw err;
}
}//org.xml.sax.ErrorHandler
);
StringReader sr = new StringReader(xmlRecords);
Debug.Msg("sr = " + sr);

InputSource is = new org.xml.sax.InputSource();
is.setCharacterStream(sr);
Debug.Msg("is = " + is);
is.close()

// turn it into an in-memory object
xmlDoc = docBuilder.parse(is);

Debug.Msg("xmlDoc = " + xmlDoc);
Debug.Msg("xmlDoc.docType = " + xmlDoc.getDoctype());

} catch(SAXParseException ex){
throw new XMLDOMLoaderException(ex.getMessage(), ex.getLineNumber());
} catch(SAXException ex){
throw new XMLDOMLoaderException(ex.getMessage());
} catch (Exception ex) {
throw new XMLDOMLoaderException(ex.getMessage());
}