It is the responsibility of the application to make the code thread safe. The validation is successful when the package com.xsd; import sax.
// parse an XML document into a DOM tree Document Builder parser = Document Builder Instance()Document Builder(); Document document = parser.parse(new File("instance.xml")); // create a Schema Factory capable of understanding WXS schemas Schema Factory factory = Schema Instance(XMLConstants.
W3C_XML_SCHEMA_NS_URI); // load a WXS schema, represented by a Schema instance Source schema File = new Stream Source(new File("my Schema.xsd")); Schema schema = Schema(schema File); // create a Validator instance, which can be used to validate an instance document Validator validator = Validator(); // validate the DOM tree try catch (SAXException e) on a parser factory.
To contrive an example, consider an XML data set that keeps track of personnel data.
The data set may include information from a tax declaration form as well as information from the employee's hiring form, with both elements named Note - The default namespace is actually a specific namespace.
It does not have any value to speak of (no name), but it is still precisely defined.
So a namespace that does have a name can never be used as the default namespace.(For SAX parsing, on the other hand, you set a property on the parser generated by the factory).Now that the program is ready to validate with an XML Schema definition, it is necessary only to ensure that the XML document is associated with (at least) one.When parsed, each element in the data set will be validated against the appropriate schema, as long as those schemas have been declared.Again, the schemas can be declared either as part of the XML data set or in the program. In general, though, it is a good idea to keep all the declarations together in one place.) To declare the schemas to use for the preceding example in the data set, the XML code would look something like the following.There are two ways to do that: , which stands for "XML Schema instance." The second line specifies the schema to use for elements in the document that do not have a namespace prefix-that is, for the elements you typically define in any simple, uncomplicated XML document.