Java xsd validation of xml without namespace

Posted by Darkflare on Stack Overflow See other posts from Stack Overflow or by Darkflare
Published on 2010-06-07T16:02:55Z Indexed on 2010/06/07 21:22 UTC
Read the original article Hit count: 253

Filed under:
|
|
|

Hi,

I want to validate an xml file against an xsd schema. The xml files root element does not have any namespace or xsi details. It has no attributes so just .

I have tried the following code from http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi.html with no luck as I receive cvc-elt.1: Cannot find the declaration of element 'root'

SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

        File schemaFile = new File("schema.xsd");

        Schema xsdScheme = factory.newSchema(schemaFile);

        Validator validator = xsdScheme.newValidator();

        Source source = new StreamSource(xmlfile);

        validator.validate(source);

The xml validates fine with the namespace headers included etc (added via xmlspy), but I would have thought the xml namespace could be declared without having to manually edit the source file?

© Stack Overflow or respective owner

Related posts about java

Related posts about Xml