Caros, uma dúvida:
Olhando o tutorial disponível, andei tentando fazer uma mudança, para que o mesmo resolvesse o meu problema. Tenho o seguinte XML:
<?xml version="1.0"?>
<CONSULTA>
<COLUNA indice="01">
<BOTAO indice="001">
<TEXTO>Associado</TEXTO>
<FUNDO>FFFFFF</FUNDO>
</BOTAO>
<BOTAO indice="002">
<TEXTO>Dependente</TEXTO>
<FUNDO>FFFFFF</FUNDO>
</BOTAO>
</COLUNA>
<COLUNA indice="02">
<BOTAO indice="007">
<TEXTO>Empresa</TEXTO>
<FUNDO>FFFFFF</FUNDO>
</BOTAO>
<BOTAO indice="002">
<TEXTO>Cidades</TEXTO>
<FUNDO>FFFFFF</FUNDO>
</BOTAO>
</COLUNA>
</CONSULTA>
É possível fazer com que, ao ler o conteúdo de COLUNA com o índice “01”, ele me retorne todos os nós internos deste nó? Nesse caso, deveria ser retornado os dados de BOTAO indice=“001” e BOTAO indice=“002”, além dos nós internos - o conteúdo de TEXTO e FUNDO…
Tentei criar o seguinte método:
public String lerBotoes() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse( this.arquivo );
org.w3c.dom.Element elem = doc.getDocumentElement();
NodeList nodeColuna = elem.getElementsByTagName("COLUNA");
for( int i=0; i<nodeColuna.getLength(); i++ ) {
org.w3c.dom.Element tagPai = (org.w3c.dom.Element) nodeColuna.item( i );
String indicePai = tagPai.getAttribute( "indice" );
NodeList nodeBotoes = elem.getElementsByTagName( "BOTAO" );
for( int j=0; j<nodeBotoes.getLength(); j++ ) {
Element tagBotao = (Element) nodeBotoes.item( j );
String indice = tagBotao.getAttribute( "indice" );
String texto = getChildTagValue( tagBotao, "TEXTO" );
String fundo = getChildTagValue( tagBotao, "FUNDO" );
System.out.println(indicePai+"\n"+indice+"\n"+texto+"\n"+fundo+"\n");
}
}
return null;
}
Porém, nesse caso, ele varre novamente todo o arquivo para cada ocorrência de COLUNA, exibindo os dados, mas de forma repetitiva.
Qualquer ajuda será bem vinda!
[]s
Rafael