Bom dia a todos do GUJ,
Estou com uma classe que faz a validação do XML da NFe através do XSD, este xml esta “ok” pois já passei no validador do SEFAZ então acredito que não seja no mesmo o erro, os arquivos XSDs requeridos estão no diretorio da aplicação (Todos disponiveis do SEFAZ), a classe que segue abaixo retirei do site da SUN disponiveis em http://java.sun.com/developer/technicalArticles/xml/validationxpath/
os schemas do sefaz usados são os do link http://www.nfe.fazenda.gov.br/portal/docs/PL_005c.zip
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class ValidaXmlSun {
/**
* @param args
*/
public static void main(String[] args) {
try {
// Parse an XML document into a DOM tree.
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new File("nota_fiscal-nfe.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("nfe_v1.10.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));
} catch (ParserConfigurationException e) {
// exception handling
System.out.println(e.getMessage());
} catch (SAXException e) {
// exception handling - document not valid!
System.out.println(e.getMessage());
System.out.println(e.toString());
System.out.println("Erro no documento");
e.printStackTrace();
} catch (IOException e) {
// exception handling
System.out.println(e.getMessage());
}
}
}
o erro que é apresentado :
cvc-elt.1: Cannot find the declaration of element ‘NFe’.
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ‘NFe’.
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ‘NFe’.
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)
at joares.com.br.validaXml.ValidaXmlSun.main(ValidaXmlSun.java:46)
Pelo que parece o erro esta no elemento “NFe” que não esta declarado, se aguem já passou por esse problema … ou tem uma classe que faz a validação e puder postar… ficarei grato … a classe de assinatura e comunicação com o webservice já fiz me falta somente a validação da estrutura do xml.
Obrigado
Joares