this is the code
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import java.io.*;
import org.xml.sax.InputSource;
import java.net.*;
import java.util.Properties;
public class Parser {
Document document = null;
String str = null;
DOMParser domParser = new DOMParser();
String strOutput = null;
public Parser() {
validateAgainstDTD();
}
public void validateAgainstDTD(){
try{
// domParser.setFeature
("http://xml.org/sax/features/validation", true);
//domParser.setFeature
("http://apache.org/xml/features/dom/defer-node-expansion",
true);
String xmlFile = "------- url for not well
formed xml------";
URL url = new URL(xmlFile);
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setUseCaches(false); // Don't use the
cache
con.setDoInput(true);
DataOutputStream out = new DataOutputStream
(con.getOutputStream());
out.writeBytes
("user=joe&password=invalid_xml");
out.close();
BufferedReader inStream=new BufferedReader
(new InputStreamReader(con.getInputStream()));
while(!(null==(strOutput=inStream.readLine
()))){
System.out.println(" "+strOutput);
}
inStream.close();
domParser.setErrorHandler(new MyErrorHandler());
domParser.parse(new InputSource(xmlFile));
Document document = domParser.getDocument();
traverse (document);
System.out.println("Well Formed");
System.out.println("well formed");
}catch (SAXNotRecognizedException e) {
System.out.println
( "SAXNotRecognizedException:" + e.getMessage());
} catch (SAXNotSupportedException e) {
System.out.println
( "SAXNotSupportedException:" + e.getMessage());
} catch (SAXException e) {
System.out.println( "SAXException:" +
e.getMessage());
} catch (IOException e) {
System.out.println( "IOException:" +
e.getMessage());
}
catch (Exception e) {
System.out.println( "Exception:" +
e.getMessage());
}
}
class MyErrorHandler implements
org.xml.sax.ErrorHandler
{
public void warning(SAXParseException e)
throws SAXException {
System.err.println("Warning : " +
e.getMessage());
String str= "Warning : " +
e.getMessage();
}
public void error(SAXParseException e) throws
SAXException {
System.err.println("Error at Line : "
+ e.getLineNumber() +" Tags not same as in dtd");
String str1= "Error at Line : " + e.getLineNumber
() +" Tags not same as in dtd";
}
public void fatalError(SAXParseException e) throws
SAXException {
System.err.println("FatalError");
String str2= "Fatal Error: " + e.getMessage() ;
}
}
private void traverse (Node node) {
int type = node.getNodeType();
if (type == Node.ELEMENT_NODE)
System.out.println (node.getNodeName()
+ " "+node.getNodeValue());
String str1=node.getNodeName();
if(str1!=null){
System.out.println(" "+node.getNodeValue());
}
NodeList children = node.getChildNodes();
if(children!=null){
for ( int i=0;i<children.getLength();i++)
traverse(children.item(i));
}
}
public static void main(String[] args){
Parser parser = new Parser();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]