oi galera,
estou tentando validar um xml com um xsd. Mas já tentei de todas as formas e apresenta erro na validação como se a tag principal não existisse.
Segue minha classe q valida:
[code] public static void validaXSD() throws ParserConfigurationException, SAXException, IOException
{
// Parse an XML document into a DOM tree.
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("cancelar.xml"));
// Create a SchemaFactory capable of understanding WXS schemas.
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// Load a WXS schema, represented by a Schema instance.
Source schemaFile = new StreamSource(new File("cancelar.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");
}[/code]
Se o xml q envio:
<?xml version="1.0" encoding="UTF-8"?>
<CancelarNfseEnvio xmlns="http://www.abrasf.org.br/nfse.xsd" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.abrasf.org.br/nfse.xsd ../xsd/nfse v2 0.xsd ">
<Pedido>
<InfPedidoCancelamento Id="">
<IdentificacaoNfse>
<Numero>0</Numero>
<CpfCnpj>
<Cpf>Cpf</Cpf>
</CpfCnpj>
<InscricaoMunicipal>InscricaoMunicipal</InscricaoMunicipal>
<CodigoMunicipio>0</CodigoMunicipio>
</IdentificacaoNfse>
<CodigoCancelamento>0</CodigoCancelamento>
</InfPedidoCancelamento>
<ds:Signature Id="idvalue0">
<ds:SignedInfo Id="idvalue1">
<ds:CanonicalizationMethod Algorithm="http://tempuri.org"/>
<ds:SignatureMethod Algorithm="http://tempuri.org">
<ds:HMACOutputLength>0</ds:HMACOutputLength>
</ds:SignatureMethod>
<ds:Reference Id="idvalue2" Type="http://tempuri.org" URI="http://tempuri.org">
<ds:Transforms>
<ds:Transform Algorithm="http://tempuri.org"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://tempuri.org"/>
<ds:DigestValue>MA==</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue Id="idvalue3">MA==</ds:SignatureValue>
<ds:KeyInfo Id="idvalue4">
<ds:KeyName>ds:KeyName</ds:KeyName>
</ds:KeyInfo>
<ds:Object Encoding="http://tempuri.org" Id="idvalue5" MimeType="">
<ds:SPKIData>
<ds:SPKISexp>MA==</ds:SPKISexp>
</ds:SPKIData>
</ds:Object>
</ds:Signature>
</Pedido>
</CancelarNfseEnvio>
o erro que dá:
Exception in thread "main" org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'CancelarNfseEnvio'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Unknown Source)
reparem q no xml ta indo a tag. Mas ele fica apresentando erro como se não tivesse. Alguem sabe porque? :shock:
thank’s