aspose file tools*
The moose likes XML and Related Technologies and the fly likes resolving external DTD Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "resolving external DTD" Watch "resolving external DTD" New topic
Author

resolving external DTD

Ravikumar Jambunathan
Ranch Hand

Joined: Aug 25, 2003
Posts: 66
Hi all,

I have written splitting and parsing program in java for a XML wrapped file. My program will split the CDATA section content using SAX parser then the XML wrapper will be parsed and loaded using DOM parser.

Everything working fine until it reaches DOM. My dtd is residing externally. I am using resolveEntity method for SAX to resolve external dtd. But for DOM, it expects the DTD to be in the current working directory. Since the dtd resides externally, i am getting FileNotFoundException.

Can anybody tell me how to resolve this problem?

thanks in advance.

regards,
ravi.

Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
While I have not tried to solve this with an example, I think you should
setEntityResolver (UML didn't allow me to post the link here) on the
DocumentBuilder.

Could you post your code snippet.....

- m
Ravikumar Jambunathan
Ranch Hand

Joined: Aug 25, 2003
Posts: 66
Hi Madhav,

Thanks for your feedback.

Here is the code,

protected String stripMessage(String filename) throws XMLException {
try {
File newFile1 = new File(filename+".strip1");
metaDataFile = newFile1.getAbsolutePath();

File newFile2 = new File(filename+".strip2");
messageFile = newFile2.getAbsolutePath();

// Perform stripping of header using SAX Parser
XMLFileSplitter xmlFileSplitter = new XMLFileSplitter();
xmlFileSplitter.setDestFile(metaDataFile);
xmlFileSplitter.setXMLHeader(filename);
xmlFileSplitter.setDataFile(messageFile);
xmlFileSplitter.setMessageTag(messageTag);
if (!xmlDTDPath.trim().equals("")) xmlFileSplitter.setDTDPath
(xmlDTDPath);

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(filename, xmlFileSplitter);
Log.info("File contains meta data = " + metaDataFile);
Log.info("File name contains message = " + messageFile);
Log.info("End stripMessage()");
} catch (SAXParseException se){
Log.error("SAXParseException: " + se.getMessage());
throw new XMLException ("","Error parsing xml file using SAX
parser",+ se);
} catch(SAXException s){
Log.error("SAXException: " + s.getMessage());
} catch(ParserConfigurationException pe){
Log.error("ParserConfigurationException: " + pe.getMessage());
} catch(IOException io){
Log.error("IOException: " + io.getMessage());
} catch(Exception e){
e.printStackTrace();
Log.error("Exception: " + e.getMessage());
}
return metaDataFile;
}

Here is the XMLFileSplitter for resolve entity,

public InputSource resolveEntity(String publicId, String systemId)
throws SAXException
{
Log.info("Configure DTD Path...");
Log.info("PublicID = " + publicId);
Log.info("SystemID = " + systemId);
//int x = systemId.lastIndexOf(File.separator);

// change dtd definition only if xmldtdPath is supplied
if ((xmlDtdPath != null) && !(xmlDtdPath.trim().equals(""))) {
int x = systemId.lastIndexOf("/");
String dtdname = systemId.substring(x+1, systemId.length());
Log.info("DTDName = " + dtdname);
getFileSep();
String newDTDurl = "file:\\" + xmlDtdPath + fileSep + dtdname;
Log.info("New DTDPath = " + newDTDurl);
return new InputSource(newDTDurl);
}
else
{
return new InputSource(systemId);
}

thanks in advance for your time.

best regards,
ravi.


[ November 05, 2004: Message edited by: Ravikumar Jambunathan ]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
So where exactly in the code is this DOM parser thingy you mentioned in your original post....

- m
Ravikumar Jambunathan
Ranch Hand

Joined: Aug 25, 2003
Posts: 66
hooo... no... extremely sorry, i will post it soon...

thanks for your time.

regards,
ravi.
Ravikumar Jambunathan
Ranch Hand

Joined: Aug 25, 2003
Posts: 66
hi,

here is the code which calls DOM parser.

public XMLBase(String filename, String dtdPath)
throws XMLException {
try {
xmlDTDPath = dtdPath;
Log.info("XML Dtd path = " + xmlDTDPath);

// Remove message from XMLWrapper
String xmlFiletoLoad = filename;
xmlFiletoLoad =stripMessage(filename);
Log.info ("xmlFiletoLoad : " + xmlFiletoLoad);

// Parse xml document and store as a DOM object
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",true);


DocumentBuilder db = null;
db= dbf.newDocumentBuilder();
xmlDocument = db.parse(xmlFiletoLoad);


/*
// Remove intermediate file after processing
if (!xmlFiletoLoad.equalsIgnoreCase(filename)) {
new File(xmlFiletoLoad).delete();
} */

} catch(ParserConfigurationException pConEx){
Log.error("ParserConfigurationException: "+pConEx.getMessage());
throw new XMLException("", "Unable to parse xml file", pConEx);
} catch(IOException ioExp){
Log.error("IOException: " +ioExp);
throw new XMLException("", "Error performing IO operation on xml file", ioExp);
} catch(SAXException saxEx){
Log.error("SAXException: " + saxEx);
throw new XMLException("", "Error parsing xml file", saxEx);
}
Log.info("XML File loaded = " + filename);
}

with best regards,
ravi.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

DocumentBuilder db = null;
db = dbf.newDocumentBuilder();
xmlDocument = db.parse(xmlFiletoLoad);


Set the EntityResolver on the DocumentBuilder.

er= new EntityResolver();
db.setEntityResolver(er);

This is what I was suggesting that you should be doing.

- m
Ravikumar Jambunathan
Ranch Hand

Joined: Aug 25, 2003
Posts: 66
Hi Madhav,

Extremely sorry for the delayed reply. Thankyou very much for your suggestion. It works.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: resolving external DTD