Olá eu estou trabalhando com XML utilizando a API JDOM, foi bem tranquilo.
Mas estou tendo um aviso ao mandar validar meu xml apartir do meu schema.
Aviso: SchemaLocation: schemaLocation value = ‘Schema.xsd’ must have even number of URI’s.
Ele executa normalmente, valida todos os campos, mas este warning ai ta me enxendo o saco por nao sei exatamente de onde vem, pelas minhas googladas percebi que se trata de namespace, mas o problema é que estou criando o xml com o atributo noNamespaceSchemaLocation, e com este atributo especifico nao achei nada na internet como possivel solução.
Vou postar meu xml, meu schema e meu codigo fonte, lembrando que comecei a mexer com XML a cerca de 1 semana, entao sou bem inexperiente neste assunto. Tudo que sei ate agora foi o compilado do que fui lendo na internet neste periodo (me fez uma falta um bom livro).
O codigo que estou usando para a criacao do XML e sua validacao.
public class ConstrucaoXML {
public ConstrucaoXML() {
}
/**
* Constroi o XML apartir da lista de blocos, onde cada bloco representa
* um nodo no nivel subsequente ao nodo principal (root)
* @param blocos conjunto de dados que seram escritos no XML
* @param nomeNodoPrincipal nome do principal elemento do XML (root)
* @return
*/
public static Element construirXML(Bloco[] blocos, String nomeNodoPrincipal) {
Element nodoPrincipal = new Element(nomeNodoPrincipal);
Namespace namespace = Namespace.getNamespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance");
nodoPrincipal.addNamespaceDeclaration(namespace);
nodoPrincipal.setAttribute(new Attribute("noNamespaceSchemaLocation",
"Schema.xsd", namespace));
for (int i = 0; i < blocos.length; i++) {
Element nodoBloco = new Element(blocos[i].getNome());
for (int k = 0; k < blocos[i].getNCampos(); k++) {
ItemArvore item = blocos[i].getCampo(k);
Element nodoCampo = new Element(item.tagNome);
nodoCampo.setText(item.valor);
nodoBloco.addContent(nodoCampo);
}
nodoPrincipal.addContent(nodoBloco);
}
return nodoPrincipal;
}
/**
* Salva o XML em um arquivo
* @param nodoPrincipal O nodo raiz (root) com todas suas dependencias
* @param nomeArquivo Nome do arquivo que sera salvo
* @throws java.io.IOException
* @throws org.jdom.JDOMException
*/
public static void salvarArquivo(Element nodoPrincipal,
String nomeArquivo) throws IOException, JDOMException {
Document doc = new Document();
doc.setRootElement(nodoPrincipal);
XMLOutputter xout = new XMLOutputter();
FileWriter arquivo = new FileWriter(nomeArquivo);
xout.output(doc, arquivo);
SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser",
true);
saxBuilder.setFeature("http://xml.org/sax/features/validation", true);
saxBuilder.setFeature("http://apache.org/xml/features/validation/schema",
true);
saxBuilder.setFeature(
"http://apache.org/xml/features/validation/schema-full-checking", true);
saxBuilder.setProperty(
"http://apache.org/xml/properties/schema/external-schemaLocation",
"Schema.xsd");
saxBuilder.setErrorHandler(new Erro());
try {
saxBuilder.build(nomeArquivo);
} catch (JDOMException ex) {
Logger.getLogger(ConstrucaoXML.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static class Erro implements ErrorHandler {
public void warning(SAXParseException exception) throws SAXException {
System.out.println("Aviso: " + exception.getMessage() +
"\nLinha: " + exception.getLineNumber() + "\tColuna: " +
exception.getColumnNumber());
}
public void error(SAXParseException exception) throws SAXException {
System.out.println("Erro - " + exception.getMessage() +
"\nLinha: " + exception.getLineNumber() + "\tColuna: " +
exception.getColumnNumber());
}
public void fatalError(SAXParseException exception) throws SAXException {
System.out.println("Erro fatal: " + exception.getMessage() +
"\nLinha: " + exception.getLineNumber() + "\tColuna: " +
exception.getColumnNumber());
}
}
}
o XML que é construido a partir desta classe.
<?xml version="1.0" encoding="UTF-8"?>
<RESULTADO_SVA xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Schema.xsd">
<DESCRICAO_DOS_DADOS>
<CABECALHO>54 49 42 43 33 </CABECALHO>
<BIN>43 45 4E 54 45 52 20 42 49 4E 3D 34 30 39 36 30 33 </BIN>
<FILE_DATE>4A 55 4C 49 41 4E 20 44 41 54 45 3D 30 37 32 31 38 </FILE_DATE>
<SEQ>46 49 4C 45 20 53 45 43 55 45 4E 43 45 3D 30 30 30 31 </SEQ>
<PROD>52 55 4E 4D 4F 44 45 3D 50 52 4F 44 </PROD>
<NUM_REG>4E 55 4D 20 52 45 43 4F 52 44 53 3D 30 30 30 30 30 30 30 34 </NUM_REG>
<NOME_MEMBRO>59 47 31 54 54 41 47 45 20 30 38 2F 30 37 2F 30 37 20 31 34 3A 31 38 3A 31 32 </NOME_MEMBRO>
<FIM_REG>0D 0A </FIM_REG>
</DESCRICAO_DOS_DADOS>
<DADOS_DO_CARTAO>
<NUM_REG>30 30 30 30 30 30 30 31 </NUM_REG>
<SEPARADOR>20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 </SEPARADOR>
</DADOS_DO_CARTAO>
</RESULTADO_SVA>
E o schema que esta sendo usado para validar este xml. É bem simples so se atem ao tamanho de cada campo e algums valores fixos
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="RESULTADO_SVA">
<xs:complexType>
<xs:sequence>
<xs:element name="DESCRICAO_DOS_DADOS">
<xs:complexType>
<xs:sequence>
<xs:element name="CABECALHO">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value = "15"/>
<xs:pattern value="54 49 42 43 33 "/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="BIN">
<xs:simpleType>
<xs:restriction base= "xs:string">
<xs:length value = "51"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="FILE_DATE">
<xs:simpleType>
<xs:restriction base= "xs:string">
<xs:length value = "51"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="SEQ">
<xs:simpleType>
<xs:restriction base= "xs:string">
<xs:length value = "54"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="PROD">
<xs:simpleType>
<xs:restriction base= "xs:string">
<xs:length value = "36"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="NUM_REG">
<xs:simpleType>
<xs:restriction base= "xs:string">
<xs:length value = "60"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="NOME_MEMBRO">
<xs:simpleType>
<xs:restriction base= "xs:string">
<xs:length value = "78"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="FIM_REG">
<xs:simpleType>
<xs:restriction base= "xs:string">
<xs:length value = "6"/>
<xs:pattern value= "0D 0A "/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="DADOS_DO_CARTAO">
<xs:complexType>
<xs:sequence>
<xs:element name="NUM_REG">
<xs:simpleType>
<xs:restriction base= "xs:string">
<xs:length value = "24"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="SEPARADOR">
<xs:simpleType>
<xs:restriction base= "xs:string">
<xs:length value = "75"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Acho que postei coisa de mais, pelo que vi pode ser apenas o cabecalho do arquivo xml que esta errado. Mas postei tudo porque eu tbem quero saber como faço para alterar a possivel solucao usando o java.
Abraço.