0

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

bobber205
  • 12,948
  • 27
  • 74
  • 100

1 Answers1

1

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

smartcaveman
  • 41,281
  • 29
  • 127
  • 212
  • Try getting the file value as a text stream and see what it writes out. It's possible you are not loading the correct file, or you are force-creating the file in a different directory than you think – smartcaveman Mar 03 '11 at 19:13
  • Weird. The issue is not with your XSD file. It must be with SaxParser (not something I know about). I retagged your question to help you get some people that can answer this – smartcaveman Mar 03 '11 at 19:28
  • I've changed my document creation to be Document document = parser.parse(new BufferedInputStream(new FileInputStream("test.xml"))); – bobber205 Mar 03 '11 at 19:38
  • It didn't. ;) Sorry worded that wrong. I meant "the tags might help". It's for sure getting all the right data as well – bobber205 Mar 03 '11 at 19:45