title: Validation XML XSD Fails
body: <p>XSD  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="windows-1252"?&gt;
  
  &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://xml.netbeans.org/schema/Person"
      xmlns:tns="http://xml.netbeans.org/schema/Person"
      elementFormDefault="qualified"&gt;
      &lt;xsd:element name="root"&gt;
          &lt;xsd:complexType&gt;
              &lt;xsd:sequence&gt;
                  &lt;xsd:element name="persons"&gt;
                      &lt;xsd:complexType&gt;
                          &lt;xsd:sequence&gt;
                              &lt;xsd:element name="person" maxOccurs="unbounded"&gt;
                                  &lt;xsd:complexType&gt;
                                      &lt;xsd:sequence&gt;
                                          &lt;xsd:element name="name" type="xsd:string"&gt;&lt;/xsd:element&gt;
                                          &lt;xsd:element name="age" type="xsd:int"&gt;&lt;/xsd:element&gt;
                                          &lt;xsd:element name="address" type="xsd:string"&gt;&lt;/xsd:element&gt;
                                      &lt;/xsd:sequence&gt;
                                  &lt;/xsd:complexType&gt;
                              &lt;/xsd:element&gt;
                          &lt;/xsd:sequence&gt;
                      &lt;/xsd:complexType&gt;
                  &lt;/xsd:element&gt;
              &lt;/xsd:sequence&gt;
          &lt;/xsd:complexType&gt;
      &lt;/xsd:element&gt;
  &lt;/xsd:schema&gt;    
  </code></pre>
  
  <p>XML File  </p>
  
  <pre><code>&lt;root&gt;
    &lt;persons&gt;
      &lt;person&gt;
        &lt;name&gt;name1&lt;/name&gt;
        &lt;age&gt;1&lt;/age&gt;
        &lt;address&gt;abc abc abc1&lt;/address&gt;
      &lt;/person&gt;
      &lt;person&gt;
        &lt;name&gt;name2&lt;/name&gt;
        &lt;age&gt;2&lt;/age&gt;
        &lt;address&gt;abc abc abc2&lt;/address&gt;
      &lt;/person&gt;
      &lt;person&gt;
        &lt;name&gt;name2&lt;/name&gt;
        &lt;age&gt;3&lt;/age&gt;
        &lt;address&gt;abc abc abc3&lt;/address&gt;
      &lt;/person&gt;
    &lt;/persons&gt;
  &lt;/root&gt;  
  </code></pre>
  
  <p>Code that validates XML against XSD: </p>
  
  <pre><code> public static String validateXMLwithXSD(String xmlFile, String xsdFile) {
          String result = "SUCCESS";
          try {
              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
              System.out.println("DocumentBuilderFactory: " + factory.getClass().getName());
              factory.setNamespaceAware(true);
              factory.setValidating(true);
              factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
              // Specify our own schema - this overrides the schemaLocation in the xml file
              factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", "d:\\person.xsd");
              DocumentBuilder builder = factory.newDocumentBuilder();
              builder.setErrorHandler(new SimpleErrorHandler());
              Document document = builder.parse(xmlFile);
              Node rootNode = document.getFirstChild();
              System.out.println("Root node: " + rootNode.getNodeName());
          } catch (Exception ex) {
              System.out.println("--------------------");
              result = "FAILURE:" + ex.getMessage();
              ex.printStackTrace();
          }catch (Error ex) {
              System.out.println("--------------------");
              result = "FAILURE:" + ex.getMessage();
              ex.printStackTrace();
          }
  
          return result;
  
      }
  </code></pre>
  
  <p>It throws   </p>
  
  <pre><code>DocumentBuilderFactory: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
  org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'root'.
          at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)  
  </code></pre>
  
  <p>Update: 
  After Davis's answer </p>
  
  <pre><code>[exec:exec]
  DocumentBuilderFactory: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
  org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'persons'. One of '{"http://xml.netbeans.org/schema/Person":persons}' is expected.
          at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)  
  
  
  error: cvc-complex-type.2.4.a: Invalid content was found starting with element 'persons'. One of '{"http://xml.netbeans.org/schema/Person":persons}' is expected.
  </code></pre>