Ler xml

0 respostas
S

Boa tarde pessoal,

Tenho um xml que preciso ler em uma classe java e vi alguns tutoriais,
como o meu xml é um pouco mais complexo, não consegui resolver o problema.
Esta uma classe de exemplo que estava dando certo, mas pq as tags do xml estava simples, com os mesmos nomes dos atributos da classe:

1. import java.util.List;  
   2. import org.jdom.Document;  
   3. import org.jdom.Element;  
   4. import org.jdom.input.SAXBuilder;  
   5.   
   6. public class LeXML {  
   7.     @SuppressWarnings("unchecked")  
   8.     public static void main(String[] args) {  
   9.         Document doc = null;  
  10.         SAXBuilder builder = new SAXBuilder();  
  11.         try {  
  12.             doc = builder.build("c:/AgroLote1.XML");      
  13.         } catch (Exception e) {  
  14.             e.printStackTrace();  
  15.         }  
  16.         Element agenda = doc.getRootElement();  
  17.         List<Element> lista = agenda.getChildren();  
  18.         for (Element e : lista) {             
  19.             System.out.println("Id: " + e.getAttributeValue("ID_NOME"));  
  21.             System.out.println("Nome: " + e.getChildText("NOME"));  
  22.             System.out.println("Dt nasc: " + e.getChildText("DATA_NASC"));  
  23.             System.out.println("Tel Res: " + e.getChildText("TEL_RESIDENCIAL"));  
  24.             System.out.println("Tel Com: " + e.getChildText("TEL_COMERCIAL"));            
  25.             System.out.println("Celular: " + e.getAttributeValue("CELULAR"));  
  26.             System.out.println("Fax: " + e.getChildText("FAX"));  
  27.             System.out.println("Endereco: " + e.getChildText("ENDERECO"));  
  28.             System.out.println("CEP: " + e.getChildText("CEP"));  
  29.             System.out.println("Bairro: " + e.getChildText("BAIRRO"));  
  30.             System.out.println("Cidade: " + e.getAttributeValue("CIDADE"));  
  31.             System.out.println("UF: " + e.getChildText("UF"));  
  32.             System.out.println("E-mail: " + e.getChildText("E_MAIL"));  
  33.             System.out.println("Internet: " + e.getChildText("INTERNET"));  
  34.             System.out.println("Obs: " + e.getChildText("OBS"));  
  35.             System.out.println("\n");  
  36.         }  
  37.     }  
  38. }

e este é o meu xml:

1. <?xml version="1.0"?>  
   2. <?mso-application progid="Excel.Sheet"?>  
   3. <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"  
   4. xmlns:o="urn:schemas-microsoft-com:office:office"  
   5. xmlns:x="urn:schemas-microsoft-com:office:excel"  
   6. xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"  
   7. xmlns:html="http://www.w3.org/TR/REC-html40">  
   8. <Worksheet ss:Name="Plan1">  
   9. <Table>  
  10. <Row>  
  11.     <Cell><Data ss:Type="String">Z</Data></Cell>  
  12.     <Cell><Data ss:Type="String">CÓDIGO NCM - ATUAL </Data></Cell>  
  13.     <Cell><Data ss:Type="String">CÓDIGO INTERNO </Data></Cell>  
  14.     <Cell><Data ss:Type="String">DESCRIÇÃO DA MERCADORIA (SEM ABREVIATURA)</Data></Cell>  
  15.     <Cell><Data ss:Type="String">INFORMAÇÕES COMPLEMENTARES</Data></Cell>  
  16.     <Cell><Data ss:Type="String">DÚVIDAS TÉCNICAS</Data></Cell>  
  17.     <Cell><Data ss:Type="String">RESPOSTAS</Data></Cell>  
  18. </Row>  
  19. <Row>  
  20.     <Cell><Data ss:Type="String">151</Data></Cell>  
  21.     <Cell><Data ss:Type="String">8482.10.90</Data></Cell>  
  22.     <Cell><Data ss:Type="String">10644</Data></Cell>  
  23.     <Cell><Data ss:Type="String">ROLAMENTO ÁRVORE PRIM CÂMBIO MF 834850M1</Data></Cell>  
  24.     <Cell><Data ss:Type="String">ROLAMENTO ; ÁRVORE ; PRIMÁRIA CÂMBIO MF 95/95X</Data></Cell>  
  25.     <Cell><Data ss:Type="String">Que tipo de rolamento? De carga radial?</Data></Cell>  
  26.     <Cell><Data ss:Type="String"></Data></Cell>  
  27. </Row>  
  28. </Table>  
  29. </Worksheet>  
  30. </Workbook>

Alguém tem idéia de como poderei ler este arquivo?

Criado 24 de setembro de 2007
Respostas 0
Participantes 1