Some Huge Basic Thing Wrong -- "Cannot Find Element X"

152 views Asked by At

My xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<credits> 
</credits>

My schema

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="credits">

  </xsd:element>

</xsd:schema>

And it says "SaxParseException: cannot find declaration of element 'credits'"

What?! :P How is that possible? Absolutely confounded here. Been googling for hours nothing yet.

Thanks SO!

        SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
        Schema schemaXSD = schemaFactory.newSchema( new File ( "test.xsd" ) );

        Validator v = schemaXSD.newValidator();
       DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  Document document = parser.parse( new File( "test.xml" ) );
        document.toString();
        DOMSource testSource = new DOMSource(document);
        v.validate( testSource );

EDIT: Found the solution. Google finally yielded something. :P I had to add

factory.setNamespaceAware(true);

to my DocumentBuilderFactory object. :D

1

There are 1 answers

4
smartcaveman On BEST ANSWER

means that the test.xml doesn't have a credits element. If this is acceptable, set minOccurs="0"