• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

resolving external DTD

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So where exactly in the code is this DOM parser thingy you mentioned in your original post....

- m
 
Ravikumar Jambunathan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hooo... no... extremely sorry, i will post it soon...

thanks for your time.

regards,
ravi.
 
Ravikumar Jambunathan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Madhav,

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

 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic