How to get Java to read in XML file and parse it against DTD?

S

Stuart Miller

When using Java to read in an XML file, I am having problems getting
the XML file to be parsed against the specified DTD.


Background:
I have an existing Java application that allows users to modify values
in a database.

To improve performance - we're looking to enable multiple record
modification by using XML files. The user downloads the required
records, changes them and uploads the modified file.

Issue:
I want to ensure that when we read the XML file in, it is parsed
against the specified DTD. The XML file includes the correct DTD. The
application throws FileNotFoundException when the DTD cannot be found,
but that seems to be it. It doesn't compare the elements in the XML
file to the DTD.

Any help would be greatly appreciated

Stuart


Here is the code, XML & DTD

/**
* Java code
*/
import java.io.FileReader;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class XML_Handler extends XMLFilterImpl {
public XML_Handler() {
super();
}

public static void readFile(java.io.File f) {
System.out.println("XML_Handler.readFile");
try {
XMLReader xr = XMLReaderFactory.createXMLReader();
XML_Handler handler = new XML_Handler();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);
xr.setDTDHandler(handler);

FileReader r = new FileReader(f);
xr.parse(new InputSource(r));
} catch (Exception e) {
System.err.println("XML_Handler.readFile " + e);
e.printStackTrace();
}
}

/**
* Additional code not included
*/
}


/**
* Example XML file
*/
<?xml version="1.0"?>
<!DOCTYPE poem SYSTEM "http://.../poem.dtd">
<poem>
<title>Title of the peom</title>
<l>line 1</l>
<l>line 2</l>
<line>line 3</line> <!-- this is an error -->
<l>line 4</l>
</poem>

/**
* Example DTD file: "http://.../poem.dtd"
*/
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT poem (title, l*)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT l (#PCDATA)>
 
C

Cid

When using Java to read in an XML file, I am having problems getting
the XML file to be parsed against the specified DTD.

2 things to check:

1. Does your SAX handler class override the appropriate methods to
receive notification of DTD issues?

error()
fatalError()
warning()

2. Are you using a validating Parser?


Here's a sample that loads your xml & dtd and gives the warning you
want.

---------------------- output ------------------------

*** start document ***
element: poem
element: title
element: l
element: l
error: Element "poem" does not allow "line" here.
error: Element type "line" is not declared.
element: line
element: l
*** end document ***



-------------------- code ---------------------

public class test {
public static void main(String args[]) {
try {

SAXParser p ;
SAXParserFactory f ;

f = SAXParserFactory.newInstance() ;
f.setValidating(true) ;

p = f.newSAXParser() ;
p.parse("input.xml", new MyHandler() ) ;

} catch (Exception e) {
System.err.println("trouble: " + e.getMessage() );
}
}
}


class MyHandler extends DefaultHandler {


public void startElement(String uri, String localName, String
qName,
Attributes attributes) throws SAXException {

String name = localName ;
if ( name.equals("") ) name = qName ;

System.out.println("element: " + name );
}


public void endDocument() throws SAXException {
System.out.println("*** end document ***");
}


public void error(SAXParseException e) throws SAXException {
System.out.println("error: " + e.getMessage() );
}


public void fatalError(SAXParseException e) throws SAXException {
System.out.println("fatalError: " + e.getMessage() );
}


public void startDocument() throws SAXException {
System.out.println("*** start document ***");
}


public void warning(SAXParseException e) throws SAXException {
System.out.println("warning: " + e.getMessage() );
}
}
 
S

Stuart Miller

2 things to check:
1. Does your SAX handler class override the appropriate methods to
receive notification of DTD issues?

error()
fatalError()
warning()

Yes it does.
2. Are you using a validating Parser?

I thought it was, however, by dumping my code and substituting yours -
it works fine.
Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top