fabiozoroastro 9 de fev. de 2007
Oi Luiz, vc deve estar errando em algum lugar, pois eu já fiz este tutorial que você indicou e funcionou certinho.
Luiz_Aguiar 9 de fev. de 2007
pior que eu e outro amigo, fizemos pelo menos umas 4x de maneiras diferentes, e seguinte exatamente o tutorial, e não dava certo do mesmo jeito.
fabiozoroastro 9 de fev. de 2007
Mas eu to vendo o seu xml aqui e está diferente do tutorial. Não será algum caracter inválido q vc colocou não?
Deve ser alguma coisa boba.
Luiz_Aguiar 9 de fev. de 2007
Não consegui visualizar nada de estranho, apesar de achar que realmente deve ser alguma coisa muto tosca que tá errada.
dudaskank 9 de fev. de 2007
Bem, eu copiei e colei seu xml pra cá e fiz um projetinho aqui pra testar, e funcionou corretamente.
Veja se ajuda em algo:
Tabela.java
package teste.guj.xml ;
import java.util.ArrayList ;
import java.util.Collection ;
public class Tabela {
String nome ;
Collection < Registro > registros ;
public Tabela ( String nome ) {
this . nome = nome ;
registros = new ArrayList < Registro > ();
}
public String getNome () {
return nome ;
}
public void setNome ( String nome ) {
this . nome = nome ;
}
public Collection < Registro > getRegistros () {
return registros ;
}
@Override
public String toString () {
return "[nome=" + nome + ", registros=" + registros + "]" ;
}
}
Registro.java
package teste.guj.xml ;
import java.util.ArrayList ;
import java.util.Collection ;
public class Registro {
Collection < Coluna > colunas ;
public Registro () {
colunas = new ArrayList < Coluna > ();
}
public Collection < Coluna > getColunas () {
return colunas ;
}
@Override
public String toString () {
return "[colunas=" + colunas . toString () + "]" ;
}
}
Coluna.java
package teste . guj . xml ;
public class Coluna {
String nome ;
String valor ;
public String getNome () {
return nome ;
}
public void setNome ( String nome ) {
this . nome = nome ;
}
public String getValor () {
return valor ;
}
public void setValor ( String valor ) {
this . valor = valor ;
}
public Coluna ( String nome , String valor ) {
this . nome = nome ;
this . valor = valor ;
}
public Coluna () {
}
@ Override
public String toString () {
return "[nome=" + nome + ", valor=" + valor + "]" ;
}
}
TabelaXMLReader.java
package teste.guj.xml ;
import java.io.IOException ;
import java.util.HashMap ;
import java.util.Map ;
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
import javax.xml.parsers.ParserConfigurationException ;
import org.w3c.dom.Document ;
import org.w3c.dom.Element ;
import org.w3c.dom.NodeList ;
import org.xml.sax.SAXException ;
public class TabelasXMLReader {
String fileName ;
public TabelasXMLReader ( String fileName ) {
this . fileName = fileName ;
}
public Map < String , Tabela > lerTabelas () throws ParserConfigurationException , SAXException , IOException {
Map < String , Tabela > resultado = new HashMap < String , Tabela > ();
DocumentBuilderFactory dbf = DocumentBuilderFactory . newInstance ();
DocumentBuilder db = dbf . newDocumentBuilder ();
Document doc = db . parse ( fileName );
Element elem = doc . getDocumentElement ();
NodeList nl = elem . getElementsByTagName ( "tabela" );
for ( int i = 0 ; i < nl . getLength (); i ++ ) {
Element tagTabela = ( Element ) nl . item ( i );
String nomeTabela = tagTabela . getAttribute ( "nome" );
Tabela tabela = new Tabela ( nomeTabela );
NodeList registros = tagTabela . getElementsByTagName ( "registro" );
for ( int j = 0 ; j < registros . getLength (); j ++ ) {
Element tagRegistro = ( Element ) nl . item ( j );
Registro registro = new Registro ();
NodeList colunas = tagRegistro . getElementsByTagName ( "coluna" );
for ( int k = 0 ; k < colunas . getLength (); k ++ ) {
Element tagColuna = ( Element ) colunas . item ( k );
String nome = tagColuna . getAttribute ( "nome" );
String valor = tagColuna . getAttribute ( "valor" );
Coluna coluna = new Coluna ( nome , valor );
registro . getColunas (). add ( coluna );
}
tabela . getRegistros (). add ( registro );
}
resultado . put ( nomeTabela , tabela );
}
return resultado ;
}
public static void main ( String [] args ) throws Exception {
TabelasXMLReader reader = new TabelasXMLReader ( "bin/teste/guj/xml/tabelas.xml" );
System . out . println ( reader . lerTabelas ());
}
}
Espero que ajude…
Luiz_Aguiar 9 de fev. de 2007
Puxa, perfeito Eduardo… valeu cara !!!
Luiz_Aguiar 9 de fev. de 2007
Cara só um errinho que encontrei na linha:
Element tagRegistro = ( Element ) nl . item ( j );
Onde ta o nl.item(j), tem que ser registros.item(j) .
dudaskank 9 de fev. de 2007
Luiz Aguiar:
Cara só um errinho que encontrei na linha:
Element tagRegistro = ( Element ) nl . item ( j );
Onde ta o nl.item(j), tem que ser registros.item(j) .
;)
ops… hehehe, verdade :oops: