Validar xml com schema xsd

0 respostas
J

oi galera

to tentando fazer meu xml ser validado com o xsd....e criei uma classe para fazer essa validacao. Minha classe tenta validar, mas sempre da erro apresentando que a tag principal não foi declarada.
E eu gerei o xml pelo netbeans baseado no xsd. E a tag existe.

Segue minha classe de validaçao:
public static void validando(String xml, String xsd) throws ParserConfigurationException, SAXException, IOException
    {
             // Parse an XML document into a DOM tree.
             DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                         
                       
             Document document = parser.parse(new File(xml));

             // Create a SchemaFactory capable of understanding WXS schemas.
             SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
             //SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.DEFAULT_NS_PREFIX);
             //SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);

             // Load a WXS schema, represented by a Schema instance.
             String caminho = "F:\\Projetos\\Web Service\\xsd 2.0\\";
             Source schemaFile = new StreamSource(new File(caminho + "nfse v2 0.xsd"));
             Schema schema = factory.newSchema(schemaFile);

             // Create a Validator object, which can be used to validate
             // an instance document.
             Validator validator = schema.newValidator();

             // Validate the DOM tree.
             validator.validate(new DOMSource(document));

             System.out.println("XML validado com sucesso");   

    }
E o erro dado:
Exception in thread "main" org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'CancelarNfseEnvio'.
	at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
	at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
	at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
	at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
	at org.apache.xerces.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
	at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
	at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
	at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
	at javax.xml.validation.Validator.validate(Unknown Source)
E o xml gerado de acordo com o xsd:
<?xml version="1.0" encoding="UTF-8"?>
<CancelarNfseEnvio  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
   xmlns:ns1='http://www.w3.org/2000/09/xmldsig#'
   xmlns='http://www.abrasf.org.br/nfse.xsd'
   xsi:schemaLocation='http://www.abrasf.org.br/nfse.xsd nfsev20.xsd'>
    <Pedido>
        <InfPedidoCancelamento Id="">
            <IdentificacaoNfse>
                <Numero></Numero>
                <CpfCnpj>
                    <Cpf></Cpf>
                </CpfCnpj>
                <InscricaoMunicipal></InscricaoMunicipal>
                <CodigoMunicipio></CodigoMunicipio>
            </IdentificacaoNfse>
            <CodigoCancelamento></CodigoCancelamento>
        </InfPedidoCancelamento>
</CancelarNfseEnvio>

alguem sabe pq? nao valida de jeito nenhum....como se não tivesse a tag cancelarNfse e na verdade, existe.
:shock: :?:

Criado 2 de novembro de 2011
Respostas 0
Participantes 1