Hi all, I'm starting a project where I have to create a Java application that outputs XML data. I want to have a check button where the user can check the validity of their input against an existing XML Schema. So if they forget to put a value into a mandatory field, the application will tell them what's missing and won't let them output the data into XML until it is valid. Also, I want them to be able to load an XML file into the application so that the fields are filled automatically if the file is valid. Can someone tell me which APIs I need and where I should start looking/reading? Thanks, Tony Brown
You should do a search on this forum for "schema validation" or something similar. You'll find that it is rather simple to validate a document against a given XML schema. However, I am unable to advice you regarding your "fill in the blanks" requirement (the only way I can think of is to write a lot of code to check for fields one at a time, manually)...
For generating XML from databases check out the OpenSource FormattedDataSet API (http://www.fdsapi.com). It can also be used to collect Form data and put it into XML format. Sample code to do this follows:
Everything I'm seeing seems to show validation by command line against an existing xml file. I would like to validate dynamically against xml data in memory (such as in a DOM tree) before I write it out to a file. Is this possible?
Joined: Jan 23, 2002
There's always a couple of alternative parsing methods -- if the example uses a File or a FileInputStream to create a Source, you can most certainly construct the Source from in-memory data structures. Browse the java.io.* javadocs. There's an example of schema validation using Xerces in our JavaRanch Wiki. If you happen to use a recent enough version of JAXP, you can rely on its implementation-agnostic schema validation features. See here and here for details.