
Please help to tell me a logic to parse an xml as per mentioned dtd inside xml. I've used SAX and DOM both but that only check for well formed xml tags but don't validate as per DTD. please help

Please visit the following link:

Many Thanks for your help, I've changed little in code and found minimized code for any dtd mentioned in xml. please advice for my below commented lines.
public boolean ParseXML(String filename)
{
String DTD_PATH = c:/dtd/;
valid = true;
try{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new org.xml.sax.ErrorHandler()
{
//Ignore the fatal errors
public void fatalError(SAXParseException exception)
throws SAXException { }
//Validation errors
public void error(SAXParseException e)
throws SAXParseException {
System.out.println("Error at " +e.getLineNumber() + " line.");
m.log.info(this,"PARSING ERROR at line : "+e.getColumnNumber()+" : "+e.getMessage());
System.out.println(e.getMessage());
valid = false;
}
//Show warnings
public void warning(SAXParseException err)
throws SAXParseException{
m.log.info(this,"PARSING ERROR at line : "+err.getColumnNumber()+" : "+err.getMessage());
System.out.println(err.getMessage());
valid = false;
//System.exit(0);
}
});
Document xmlDocument = builder.parse(new FileInputStream(filename),DTD_PATH);
//Please tell me the use of below commented lines since now this code can run on any mentioned dtd in xml.
/*DOMSource source = new DOMSource(xmlDocument);
StreamResult result = new StreamResult(System.out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "c:/dtd/EMC-UPDATE.dtd");
transformer.transform(source, result); */
}
catch (Exception e) {
System.out.println("ERROR :: "+e.getMessage());
m.log.info(this,"ERROR : "+m.ctool.getException(e));
return false;
}
return valid;
}

Many Thanks for your help, I've changed little in code and found minimized code for any dtd mentioned in xml. please advice for my below commented lines.
public boolean ParseXML(String filename)
{
String DTD_PATH = c:/dtd/;
valid = true;
try{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new org.xml.sax.ErrorHandler()
{
//Ignore the fatal errors
public void fatalError(SAXParseException exception)
throws SAXException { }
//Validation errors
public void error(SAXParseException e)
throws SAXParseException {
System.out.println("Error at " +e.getLineNumber() + " line.");
m.log.info(this,"PARSING ERROR at line : "+e.getColumnNumber()+" : "+e.getMessage());
System.out.println(e.getMessage());
valid = false;
}
//Show warnings
public void warning(SAXParseException err)
throws SAXParseException{
m.log.info(this,"PARSING ERROR at line : "+err.getColumnNumber()+" : "+err.getMessage());
System.out.println(err.getMessage());
valid = false;
//System.exit(0);
}
});
Document xmlDocument = builder.parse(new FileInputStream(filename),DTD_PATH);
//Please tell me the use of below commented lines since now this code can run on any mentioned dtd in xml.
/*DOMSource source = new DOMSource(xmlDocument);
StreamResult result = new StreamResult(System.out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "c:/dtd/EMC-UPDATE.dtd");
transformer.transform(source, result); */
}
catch (Exception e) {
System.out.println("ERROR :: "+e.getMessage());
m.log.info(this,"ERROR : "+m.ctool.getException(e));
return false;
}
return valid;
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.