Validating xml in java

Regular expressions are a good way to validate text fields such as names, addresses, phone numbers, and other user information. A regular expression can easily check whether a user entered something that looks like a valid phone number.

You can use them to constrain input, apply formatting rules, and check lengths. By using capturing groups to remember each set of digits, the same regular expression can be used to replace the subject text with precisely the format you want.

If you use a DOM parser explicitly for validation, you will definitely instantiate a DOM tree. Is is a case of just creating the Error Handler and associating it with the validator? is probably a network location, so it'll go out and hit the network with each and every validation, not always optimal.

Here's an example that validates an XML file against any XSD's it references (even if it has to pull them from the network): You can avoid pulling referenced XSD's from the network, even though the xml files reference url's, by specifying the xsd manually (see some other answers here) or by using an "XML catalog" style resolver.

You can probably do that with JDK API for Xml validation, if you use SAX-based writer: if so, just link in validator by calling 'Validator.validate(source, result)', where source comes from your writer, and result is where output needs to go.


But then again, validating XML against XSD files is a rather heavy weight problem.The implementation is available in the Java, C and Perl programming languages.The name "Xerces" is believed to commemorate the extinct Xerces Blue butterfly (Glaucopsyche xerces). Regex class for validate any input string for any specific format.

The following example will allow you to Input Phone Number only.

A JAXP implementation could legally use a DOM parser in this case, but there is no reason to. Set Error Handler() as in the example in this SO question…?


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>