XSD Schema Validator on XML DOM Object - Errors

This section provides some errors from the XSD schema validation process on invalid XML files represented in Document objects using the JAXP API.

Now let test XsdSchemaDomValidatorWithErrorHandler.java with some invalid XML files. The first one has an extra node, first_html_extra_node.xml:

<?xml version="1.0" encoding="utf-8"?>
<html>
<body>
<p>My first HTML document in XML format.</p>
</body>
</html>

Here is the test result with JDK:

herong> java XsdSchemaDomValidatorWithErrorHandler \
   first_html.xsd first_html_extra_node.xml

Validator Class:
   com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl

org.xml.sax.SAXParseException; cvc-type.3.1.2: Element 'body' is a
simple type, so it must have no element information item [children].

Failed with errors: 1

The validation reported a very clear error message: the "body" element has a child element, which is not allowed by the schema.

Here is another invalid XML, first_html_extra_attribute.xml:

<?xml version="1.0" encoding="utf-8"?>
<html>
<body bgcolor="#eeeeee">
<p>My first HTML document in XML format.</p>
</body>
</html>

Here is the test result with JDK:

herong> java XsdSchemaDomValidatorWithErrorHandler \
   first_html.xsd first_html_extra_attribute.xml

Validator Class:
   com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl

org.xml.sax.SAXParseException: cvc-type.3.1.1:
Element 'body' is a simple type,
so it cannot have attributes, excepting those whose namespace name
is identical to 'http://www.w3.org/2001/XMLSchema-instance'
and whose [local name] is one of 'type', 'nil', 'schemaLocation'
or 'noNamespaceSchemaLocation'. However, the attribute, 'bgcolor'
was found.

org.xml.sax.SAXParseException; cvc-type.3.1.2: Element 'body' is a
simple type, so it must have no element information item [children].

Failed with errors: 2

Good. My validator, XsdSchemaDomValidatorWithErrorHandler.java, works on multiple errors. I like SAXParseException error messages. They give detailed information.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 XML-JSON Document Conversion

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

Validating XML Documents Against Specified XML Schemas

 XSD Schema Validator on XML DOM Object

XSD Schema Validator on XML DOM Object - Errors

 XSD Schema Validator on XML SAX Object

 SAXParseException - 'xsi:noNamespaceSchemaLocation' Not Allowed

 Validating XML Linked with XSD using XMLReader

 XSL (Extensible Stylesheet Language) Introduction

 Java Implementation of XSLT

 XSLT (XSL Transformations) Introduction

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 PHP Extensions for XML Manipulation

 Processing XML with Python Scripts

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML Plugin Packages for Atom Editor

 XML 1.1 Changes and Parsing Examples

 Archived Tutorials

 References

 Full Version in PDF/EPUB