Warning: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of [runtime function name](). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file in /home/plugmasters/www/sys/admin/includes/classes/class.install.php on line 543
Classe para leitura de arquivos XML, por Tiago Hillebrandt

  » Início » Programação » Java » Classe para leitura de arquivos XML
 
Avaliação: Não avaliado | Publicado em: 23/04/2008
Classe para leitura de arquivos XML
Tiago Hillebrandt trabalha atualmente como desenvolvedor Web e professor. Tem conhecimentos em diversas tecnologias como AJAX, Delphi, C, Firebird/Interbase, MySQL, Access, Javascript, HTML, CSS, Java, Pascal, SQL e PHP. Trabalha também com ferramentas como Corel Draw, Photoshop, Dreamweaver e Flash. Atualmente está cursando Tecnologia em Sistemas de Informação na Universidade do Estado de Santa Catarina (UDESC).


Bom dia galera, tudo bem?

Segue uma classe de leitura de arquivos XML em Java!

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XML implements iXML {
    private String arquivo;

    /**
     * Seta o arquivo XML
     *
     * @param String nome_arquivo - Nome do arquivo XML
     * @return void
     */

    public void setArquivo(String nome_arquivo) {
        this.arquivo = nome_arquivo;
    }

    /**
     * Pega o arquivo XML
     *
     * @return void
     */

    public String getArquivo() {
        return this.arquivo;
    }

    /**
     * Carrega o arquivo XML
     *
     * @return Document doc
     */

    public Document CarregarXML() throws Exception {
        DocumentBuilderFactory dbf =        DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
        Document doc = docBuilder.parse(new File(this.getArquivo()));

        return doc;
    }

    /**
     * Le a tag do arquivo XML carregado
     *
     * @param String tag - Nome da tag a ser lida no  arquivo XML
     * @return String valor
     */

    public String LerTag(String tag) throws Exception {
        String valor = “”;

        Document doc = this.CarregarXML();

        Element elemento = doc.getDocumentElement();
        Element eltag = (Element)             elemento.getElementsByTagName(tag).item(0);

        valor = eltag.getTextContent().toString();
        valor = (valor != "") ? valor : "";

        return valor;
    }
}

interface iXML {
    public void setArquivo(String nome_arquivo);

    public String getArquivo();

    public Document CarregarXML() throws Exception;

    public String LerTag(String tag) throws Exception;
}

Fim da classe ;) Para utilizar a classe, instancie-a, passe por parâmetro em setArquivo() o nome do arquivo XML e utilize o método LerTag() para ler uma tag especifica no arquivo!

XML xml = new XML(); // Cria o objeto

xml.setArquivo("NOME_DO_ARQUIVO_XML.xml");

xml.LerTag("NOME_DA_TAG_XML"); // Ex: LerTag(”channel”)


É isso galera! Espero ter ajudado ;)

Visitem também meu blog http://tiagohillebrandt.wordpress.com !

Até mais ^^