473,395 Members | 1,846 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,395 software developers and data experts.

XML Validation From Schema In Servlet Filter

MJ
I am attempting to write a ServletFilter in Java/J2EE that checks if
the incoming data is valid according to a schema. If errors occur, I
want to return the error list and not proceed down the chain. If no
errors, the next in the servlet chain should receive the original XML.

The data in the input stream has already been consumed by the XML
parser.
What is the easiest way to validate the XML and keep the source xml
available for the next link in the chain?

something like...

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// we want to stream the input stream thru a SAX validation and
check for errors
ValidationHandler handler = new ValidationHandler();
try {
XMLReader reader =
XMLReaderFactory.createXMLReader("org.apache.xerce s.parsers.SAXParser");
reader.setFeature("http://xml.org/sax/features/validation",
true);
reader.setFeature("http://apache.org/xml/features/validation/schema",
true);
reader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
"test" + test.xsd);
reader.setErrorHandler(handler);
reader.setContentHandler(handler);

InputStream inStream = request.getInputStream();
InputSource inSource = new InputSource(inStream);
reader.parse(inSource);

if (handler.hasErrors()) {
//write back errors in the response
}
else // parsed successfully now process
chain.doFilter(request, response);

}
catch (SAXException e) {
//write back errors in the response
}
}
Jul 20 '05 #1
0 1730

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

Similar topics

2
by: Tommy | last post by:
HI! I'm looking for a servlet which i can set up on my own server. it would be nice if there would be something released under gnu licence or anything like that. i want to upload a xml-file...
1
by: Mike | last post by:
Note: My XML experience to date has (unfortunately) been limited to reading and thinking, rather than implementation. Anyway, I am in the process of trying to figure out the most efficient way...
2
by: wumingshi | last post by:
Hi, When validating an XML instance, sometimes the schema is not enough to expression the validation rules. Additional validation rules may be expressed in an application-specific way. For...
2
by: Sudip Chakraborty | last post by:
Is there a way to see constraint validation errors while loading xml into a DataSet ? I'm interested in the line number in the xml file which is causing the error. I've enclosed the relevant stack...
2
by: Shone | last post by:
I would like to perform a 2-pass XML reading from a stream. Once using the Validating reader, just to confirm the validity against the schema, and next time to do a reading to extract the data....
1
by: billa1972 | last post by:
Hi, I am trying to hook into Yellow Freight's rating webservice. Below is the wsdl. When i try and create a proxy file with wsdl.exe i get the following errors, see below. Also, when i...
2
by: Marcin Cenkier | last post by:
Hi, I want to validate a DOM document, and if I build DOM from a stream using documentBuilder.parse() validation using validator.validate(DOMSource) works, but if I create the same document...
6
by: lists | last post by:
Hi all, I am trying to validate an XML file against an XSD schema file within a ..NET C++ program, but the validation doesn't seem to be occuring. My code is listed below. The validation...
7
by: bylum | last post by:
Servlet SQLException Communication link failure java howto i can't connect jsp and database(mysql). This is the exception: exception org.apache.jasper.JasperException:...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.