By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

validating xml with an external DTD

P: n/a
jmb
Can anyone help me out. I have a set of xml documents and a dtd which
is not refered to in the xml documents themselves.

I've written the code to check that the xml is well formatted using
org.apache.xerces.parsers.DOMParser and i've created my own
entityResolver().

When i try and assign the DTD to the parser using the entityResolver
the parser does not throw any error indicating that the xml is
invalid.

This is driving me mad. Any help much appreciated.

================================================== =========================
private static void parseFile(File f)
{
DOMParser parser = new DOMParser();
parser.setEntityResolver(new MyResolver());
try
{
byte[] bytes = new byte[0];

FileInputStream fis = new FileInputStream(f);
bytes = new byte[(int) f.length()];
fis.read(bytes);
fis.close();
// Turn the the byte array into a InputSource for the
parser
final ByteArrayInputStream xmlByteStream = new
ByteArrayInputStream(bytes);
final InputSource xmlInputSource = new
InputSource(xmlByteStream);

parser.parse(xmlInputSource);
final Document document = parser.getDocument();
}
catch (SAXException e)
{
System.out.println(f.getName() + " " + e);
}
catch (IOException e)
{
System.out.println(f.getName() + " " + e);
}
}
================================================== =========================
public class MyResolver implements EntityResolver
{
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException
{
File f = new File("h:\\xml\\article_dtd.dtd");
FileInputStream fis = null;
try
{
fis = new FileInputStream(f);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return new InputSource(fis);
}

}
================================================== ===========================
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Try the Transformer class to transform a StreamSource (the file you want
to check) to a DOMResult. Make sure to call setValidating(true) on the
Transformer (or the TransformerFactory, I'm not sure).

I tried to get my parser working in a similar setup as yours, but I
didn't get it right. The Transformer class is much more straightforward
to use, and has the same result: a DOM object in case the document could
be read, or a TransformerException if the validation fails (at least,
that's what I can recall as it does - check the API).

Regards,

Joost van Stuijvenberg
jmb wrote:
Can anyone help me out. I have a set of xml documents and a dtd which
is not refered to in the xml documents themselves.

I've written the code to check that the xml is well formatted using
org.apache.xerces.parsers.DOMParser and i've created my own
entityResolver().

When i try and assign the DTD to the parser using the entityResolver
the parser does not throw any error indicating that the xml is
invalid.

This is driving me mad. Any help much appreciated.

================================================== =========================
private static void parseFile(File f)
{
DOMParser parser = new DOMParser();
parser.setEntityResolver(new MyResolver());
try
{
byte[] bytes = new byte[0];

FileInputStream fis = new FileInputStream(f);
bytes = new byte[(int) f.length()];
fis.read(bytes);
fis.close();
// Turn the the byte array into a InputSource for the
parser
final ByteArrayInputStream xmlByteStream = new
ByteArrayInputStream(bytes);
final InputSource xmlInputSource = new
InputSource(xmlByteStream);

parser.parse(xmlInputSource);
final Document document = parser.getDocument();
}
catch (SAXException e)
{
System.out.println(f.getName() + " " + e);
}
catch (IOException e)
{
System.out.println(f.getName() + " " + e);
}
}
================================================== =========================
public class MyResolver implements EntityResolver
{
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException
{
File f = new File("h:\\xml\\article_dtd.dtd");
FileInputStream fis = null;
try
{
fis = new FileInputStream(f);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return new InputSource(fis);
}

}
================================================== ===========================

Jul 17 '05 #2

P: n/a
st******@hotmail.com (jmb) wrote:
Can anyone help me out. I have a set of xml documents and a dtd
which is not refered to in the xml documents themselves.

I've written the code to check that the xml is well formatted using
org.apache.xerces.parsers.DOMParser and i've created my own
entityResolver().
Why? I'm not sure that you understand what an EntityResolver is.
When i try and assign the DTD to the parser using the entityResolver
the parser does not throw any error indicating that the xml is
invalid.


Hand your parser an ErrorHandler instance that throws an exception in
error().

-- Lucas
Jul 17 '05 #3

P: n/a
I'm interested by the question. What's really a EntityResolver ?

Bernard
"Lucas Bergman" <lu***@bergmans.us> a écrit dans le message de news:
e9**************************@posting.google.com...
st******@hotmail.com (jmb) wrote:
Can anyone help me out. I have a set of xml documents and a dtd
which is not refered to in the xml documents themselves.

I've written the code to check that the xml is well formatted using
org.apache.xerces.parsers.DOMParser and i've created my own
entityResolver().


Why? I'm not sure that you understand what an EntityResolver is.
When i try and assign the DTD to the parser using the entityResolver
the parser does not throw any error indicating that the xml is
invalid.


Hand your parser an ErrorHandler instance that throws an exception in
error().

-- Lucas

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.