Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is my XML Parser:</p> <pre><code>public class XMLReaderMarca extends DefaultHandler { private static String TAG = "TAG"; /** Buffer que guarda as informações quando um texto é encontrado */ private StringBuffer valorAtual = new StringBuffer(); /** Lista que possui os objetos do arquivo XML */ private ArrayList&lt;Marca&gt; lista = new ArrayList&lt;Marca&gt;(); // private ArrayList&lt;Produto&gt; lista; public ArrayList&lt;Marca&gt; getLista() { return lista; } /** apenas para coletar as informações do XML */ private Marca objetoTemp; /** * Constutor que inicializa os objetos necessários para fazer o parse do * arquivo xml */ public void parse(String _caminho) { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = spf.newSAXParser(); File file = new File(_caminho); Log.i(TAG, " -- Inicio do XMLReaderMarca.parse --"); parser.parse(file, this); Log.i(TAG, " -- Fim do XMLReaderMarca.parse --"); file.delete(); } catch (ParserConfigurationException e) { Log.i(TAG, "XMLReaderMarca.parse: O parser não foi configurado corretamente."); } catch (SAXException e) { Log.i(TAG, "XMLReaderMarca.parse: Problema ao fazer o parse do arquivo."); } catch (IOException e) { Log.i(TAG, "XMLReaderMarca.parse: O arquivo não pode ser lido."); } } /** * Indica que o parser achou o início do documento XML. Este evento não lhe * passa qualquer informação, apenas indica que o parser vai começar a * escanear o arquivo XML. */ public void startDocument() { Log.i(TAG, "Iniciando a leitura do XML em XMLReaderMarca"); } /** * Indica que o parser achou e fim do documento XML. */ public void endDocument() { Log.i(TAG, "Finalizou a leitura do XML em XMLReaderMarca"); } /** * Indica que o parser achou o início de uma tag (tag de abertura/início). * Este evento fornece o nome do elemento, o nome e valor dos atributos * deste elemento, e também pode fornecer as informações sobre o namespace. */ public void startElement(String uri, String localName, String tag, Attributes atributos) { valorAtual.setLength(0); if (tag.equalsIgnoreCase("Marca")) { objetoTemp = new Marca(); } /* * //se o elemento possui atributos, imprime for (int i=0; i&lt; * atributos.getLength(); i++){ if * (atributos.getQName(i).equalsIgnoreCase(ATT_ID)){ * contatoTemp.setId(Integer.parseInt(atributos.getValue(i))); } else if * (atributos.getQName(i).equalsIgnoreCase(ATT_GRAVADO)){ * contatoTemp.setGravado(atributos.getValue(i)); } } */ } /** * Indica que o parser achou o fim de uma tag/elemento. Este evento fornece * o nome do elemento, e também pode fornecer as informações sobre o * namespace. */ public void endElement(String uri, String localName, String tag) { // adiciona o objeto na lista if (tag.equalsIgnoreCase("Marca")) { lista.add(objetoTemp); } // senão, seta os atributos else if (tag.equalsIgnoreCase(Marca.TAGS[0])) { objetoTemp.setCd_marca(validaStringInteger(valorAtual.toString())); } else if (tag.equalsIgnoreCase(Marca.TAGS[1])) { objetoTemp.setDs_marca(valorAtual.toString()); } // limpa o valor Atual valorAtual.delete(0, valorAtual.length()); } /** * Indica que o parser achou algum Texto (Informação). */ public void characters(char[] ch, int start, int length) { valorAtual.append(ch, start, length); } private Integer validaStringInteger(String string) { return isDigit(string) ? Integer.valueOf(string) : 0; } private Double validaStringDouble(String string) { return Double.valueOf(string); } boolean isDigit(String s) { return s.matches("[0-9]*"); } } </code></pre> <p>My "TAGs" are the field's name, with this you don't need any external libs.</p> <p>Sorry, but the code's comments is in Portuguese (Brazil), I can't translate right now, sorry :(</p> <p>[]'s Bertan</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload