473,385 Members | 2,013 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,385 software developers and data experts.

validating xml with an external DTD

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
3 6766
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: blu4899 | last post by:
Hi, The Xerces XML parser is reading external DTD references in DOCTYPEs by default, but is not doing anything with them because validation is turned off by default. This is documented in...
12
by: Peter Collinson | last post by:
Hi... My external style sheet for a 200+ page site is marked up as: BODY, P, TABLE, TD, DIV { background: #ffffff; font-style: normal; font-variant: normal; font-weight: normal; font-size:...
1
by: DP | last post by:
I am requesting xml from a url. Now I want to validate this xml by a dtd. Is it possible that i define a dtd afterwards instead of in the xml itself? I don't like to bother the users wich provide...
0
by: Aaron P Frenger | last post by:
Hello All, I have a very large XML file that I would like to split up into a few smaller files, but still use only one schema. I am using Xerces C++ libraries. My idea is to have one schema...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
4
by: Adam Smith | last post by:
Hello, How can I call or trigger an external javascript twice in a form? I have <script language="JavaScript" src="country_state.js" name="Country_State"> <script type="text/javascript"...
1
by: Lyndon | last post by:
Hi all, I am developing an Access database application that requires some additional, non-database related functionality. This additional functionality is essentially to validate a flat text...
2
by: Gnus | last post by:
Hi all! I have a little(?) problem with external schema location... I'm using xerces/xalan (C++) for doing some actions with some set of xml-files. Some of these files must validating...
0
by: Lucilue2003 | last post by:
Hello, I need help validating an xml file against an external DTD. The below code allows me to read the xml file and gather specific elements of interest. If the xml looked like this, How...
2
by: Peted | last post by:
Hi if i derive a reference to a control on a winform (ie Control activeControl = somecontrol on the form) how can i test if that control has a validating or validated event and more importantly...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.