简体   繁体   中英

How to validate XML against schema without using exceptions for control flow?

  public static boolean IsValid(InputStream xml, InputStream xsd)
  {
    try
    {
      SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
      Schema schema = factory.newSchema(new StreamSource(xsd));
      Validator validator = schema.newValidator();
      validator.validate(new StreamSource(xml));
      return true;
    }
    catch (Exception ex)
    {
      return false;
    }
  }

As you can see exceptions are used for control flow, which I want to change, since it is no good practice, as exceptions should be used only for exceptional cases. (Maybe one could argue, that it is indeed an execptional case in some contexts, if a xml is not valid... )

The validate method has anothe signature validate(Source source, Result result) , but taking a look at the Result interface did not really help me.

/**
 * Set the system identifier for this Result.
 *
 * <p>If the Result is not to be written to a file, the system identifier is optional.
 * The application may still want to provide one, however, for use in error messages
 * and warnings, or to resolve relative output identifiers.</p>
 *
 * @param systemId The system identifier as a URI string.
 */
public void setSystemId(String systemId);

/**
 * Get the system identifier that was set with setSystemId.
 *
 * @return The system identifier that was set with setSystemId,
 * or null if setSystemId was not called.
 */
public String getSystemId();

So how can I validate XMLs against Schemas? It doesn't need to be my approach! Was it intended to use validate the way I did it?

I think you should implement an ErrorHandler , for example by extending DefaultHandler , and register that to your Validator by calling Validator#setErrorHandler() .

Your ErrorHandler can than record all validation errors (and warnings, if you're interested in them). Don't forget to implement / override the fatalError(...) method as well, so that you'll be notified when the document could not be parsed, for example because it is not well-formed.

As long as your ErrorHandler doesn't throw any exceptions, neither will the call to validate() . Its Javadoc states that it will throw a SAXException

if the ErrorHandler throws a SAXException or if a fatal error is found and the ErrorHandler returns normally.

So it should be possible to validate a document and not rely on exception handling for control flow. As long as you ask your ErrorHandler whether it found any errors after having validated the document. Unfortunately, if your document is not well-formed, you'll still have to catch a SAXParseException .

The designers of the JAXP validation interface chose to signal a validation failure using an Exception. You may think that's a poor design, but your choice is to use that API the way it is designed, or to find a different API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM