Created
September 29, 2018 00:51
-
-
Save tuxcuiabano/ba15ae3446f1b210291a4fdad6ca0d3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package carregaxml; | |
/** | |
* | |
* @author tuxcuiabano | |
* Retirado do Livro Texto "Tecnologia de Dados para Internet - MACIEL, C, 2018" | |
*/ | |
import javax.xml.parsers.*; | |
import org.w3c.dom.*; | |
import java.io.File; | |
public class CarregaXML { | |
public static void main(String[] args) { | |
// O array de String args é composto pelos parâmetros | |
// passados na chamada à execução. Nesse caso, | |
// "java XML_DOM_Carrega <nomeDoArquivoXML.xml>", args contém | |
// apenas um elemento, sendo args[0] = <nomeDoArquivoXML.xml> | |
if (args.length == 0) { | |
System.err.println("Informar o arquivo XML: /" + | |
"java CarregaXML <arquivo.xml>"); | |
System.exit(1); | |
} | |
// Se o nome do documento XML foi passado como parâmetro | |
String arquivoXML = args[0]; | |
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance(); | |
DocumentBuilder db = null; | |
Document doc = null; | |
try { | |
db = dbf.newDocumentBuilder(); | |
// Nesse ponto, o parser DOM carrega o documento XML | |
// desejado e retorna um Document com sua representação | |
// em árvore que deve ser guardado | |
doc = db.parse(new File(arquivoXML)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
// A próxima linha imprime no console de saída a codificação | |
// do documento XML carregado. No caso, "UTF-8". | |
System.out.println("O documento XML " + arquivoXML + " está codificado em " +doc.getXmlEncoding()); | |
} // Fim da função main | |
} // Fim da classe XML_DOM_Carrega |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment