-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
93 lines (81 loc) · 3.22 KB
/
Copy pathindex.html
File metadata and controls
93 lines (81 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Conversor CSV para XML</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.2/papaparse.min.js"></script>
<style>
body { font-family: sans-serif; padding: 20px; line-height: 1.6; max-width: 800px; margin: auto; }
.box { border: 2px dashed #ccc; padding: 20px; text-align: center; margin-bottom: 20px; }
textarea { width: 100%; height: 200px; margin-top: 10px; font-family: monospace; }
button { background: #28a745; color: white; border: none; padding: 10px 20px; cursor: pointer; border-radius: 5px; }
button:hover { background: #218838; }
</style>
</head>
<body>
<h2>Conversor de CSV para XML</h2>
<div class="box">
<input type="file" id="csvFile" accept=".csv">
<p>Ou cole o conteúdo CSV abaixo:</p>
</div>
<button onclick="converter()">Converter e Baixar XML</button>
<h3>Resultado:</h3>
<textarea id="resultado" readonly placeholder="O XML aparecerá aqui..."></textarea>
<script>
function converter() {
const fileInput = document.getElementById('csvFile');
const file = fileInput.files[0];
if (file) {
// Se houver arquivo, lemos o arquivo
Papa.parse(file, {
header: true,
skipEmptyLines: true,
complete: function(results) {
gerarXml(results.data);
}
});
} else {
alert("Por favor, selecione um arquivo CSV.");
}
}
function gerarXml(dados) {
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n<root>\n';
dados.forEach(linha => {
xml += ' <item>\n';
for (let chave in linha) {
// Limpa o nome da tag (remove espaços) e escapa valores
let tag = chave.trim().replace(/\s+/g, '_');
let valor = escaperXml(linha[chave]);
xml += ` <${tag}>${valor}</${tag}>\n`;
}
xml += ' </item>\n';
});
xml += '</root>';
document.getElementById('resultado').value = xml;
downloadXml(xml);
}
// Função para evitar que caracteres especiais quebrem o XML
function escaperXml(unsafe) {
return String(unsafe).replace(/[<>&"']/g, function (m) {
switch (m) {
case '<': return '<';
case '>': return '>';
case '&': return '&';
case '"': return '"';
case "'": return ''';
default: return m;
}
});
}
function downloadXml(conteudo) {
const blob = new Blob([conteudo], { type: 'application/xml' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'dados_convertidos.xml';
a.click();
URL.revokeObjectURL(url);
}
</script>
</body>
</html>