473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

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)>
Jul 20 '05 #1
0 3587

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael | last post by:
Hello I am trying to write a Java-Program which converts a XML-file in a HTML. It should take the Transformation-file from the XML-file itself. Below find a possible XML-file: <?xml...
5
by: Aleksandar Matijaca | last post by:
Hi there, I am in some need of help. I am trying to parse using the apache sax parser a file that has vaid UTF-8 characters - I keep end up getting a sun.io.MalformedInputException error. ...
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
10
by: Alex Gerdemann | last post by:
Hello, I have spent a bunch of time converting a Java program I wrote to C++ in order to improve performance, and have found that it is not necessarily faster. Specifically, I'm writing a...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
0
by: tumodav | last post by:
Hi friend, This tumodav. I have created a web service. I need to pass complex type of parameter. like from client side user can send user define datatype (ex. any class type object). it will create...
2
by: wolverine | last post by:
Hi, I recently read that every java .class file, is written in a specific binary format. http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html I just want to know whether...
0
by: VeeraLakshmi | last post by:
I am doing a project for internet control using Java,PHP and MySql.All sites should go through the proxy server only.We are giving access rights as allow or deny to the sites.If we type the...
318
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.