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)>
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)>