Pessoal, é o seguinte! EU to usando o DOM para ler mensagens XML, mas dêm uma olhada num exemplo de estrutura que estou utilizando:
<?xml version=“1.0” encoding=“ISO-8859-1”?>
<PRIMITIVA nome=“MENSAGEM”>
<CLASSE nome=“ENTRADA”>
<CLASSE nome=“AUTENTICACAO”>
<CAMPO nome="USUARIO">user</CAMPO>
<CAMPO nome="SENHA">password</CAMPO>
<CAMPO nome="ORIGEM">system</CAMPO>
</CLASSE>
<CLASSE nome=“DADOS”>
<CAMPO nome="CLIENTE"></CAMPO>
<CAMPO nome="EMPRESA"></CAMPO>
<CAMPO nome="ENDERECO"></CAMPO>
<CAMPO nome="CIDADE"></CAMPO>
<CLASSE nome="Grupo_Telefones">
<CLASSE nome="Telefone">
<CAMPO nome="Telefone"></CAMPO>
<CAMPO nome="Tipo_Telefone"></CAMPO>
<CAMPO nome="Nome_Contato"></CAMPO>
</CLASSE>
</CLASSE>
</CLASSE>
</CLASSE>
<CLASSE nome=“SAIDA”>
<CLASSE nome=“MENSAGEM”>
<CAMPO nome="CODIGO"></CAMPO>
<CAMPO nome="DESCRICAO"></CAMPO>
</CLASSE>
<CLASSE nome=“ATENDENTE”>
<CAMPO nome="NOME"></CAMPO>
<CAMPO nome="SETOR"></CAMPO>
<CAMPO nome="RAMAL"></CAMPO>
</CLASSE>
</CLASSE>
</PRIMITIVA>
Meu código é mais ou menos assim:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(arquivoXML);
Element elem = doc.getDocumentElement();
NodeList nl = elem.getElementsByTagName("CLASSE");
for( int i=0; i < nl.getLength(); i++ ) {
Element classe = (Element) nl.item( i );
String nomeClasse = classe.getAttribute("nome");
NodeList children = classe.getElementsByTagName("CAMPO");
for( int j=0; j < children.getLength(); j++ ) {
Element campo = (Element) children.item(j);
String nomeCampo = campo.getAttribute("nome");
}
}
O que acontece é o seguinte:
Quando uso o primeiro comando (getElementsByTagName(“CLASSE”) ele pega todos os nos chamados CLASSE, e eu queria apenas os que estão logo abaixo do nó PRIMITIVA, mas até ai tudo bem. Porém quando uso o segundo comando para pegar os campos (getElementsByTagName(“CAMPO”);), ele também pega todos os nos chamados CAMPO do documento, e não apenas os que estão dentro do elemento que criei com o no CAMPO, conseguiram entender?
Então não sei dentro que qual nó CLASSE está o nó CAMPO…
Como posso fazer isso? Como vocês costumam tratar arquivos XML?
Valeu pessoal![/code][/quote]