
| Arquivo: dados.xml |
| <?xml version="1.0" encoding="utf-8"?> <alunos> <aluno> <nome>João</nome> <provas> <nota>8</nota> <nota>6</nota> <nota>10</nota> <nota>2</nota> </provas> </aluno> <aluno> <nome>Maria</nome> <provas> <nota>3</nota> <nota>5</nota> <nota>8</nota> <nota>1</nota> </provas> </aluno> <aluno> <nome>Pedro</nome> <provas> <nota>7</nota> <nota>6</nota> <nota>6</nota> <nota>8</nota> </provas> </aluno> </alunos> |
| Arquivo: xml.html |
| <!doctype html> <html lang="pt-br"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Untitled Document</title> </head> <body> <script> function rPrintln(message = "") { if (typeof document !== "undefined" && document.body) { document.body.innerHTML += message + "<br>"; } console.log(message); } function openResponse(textHTML, callback) { const handler = typeof callback === 'function' ? callback : (typeof rOpenFileWebJS === 'function' ? rOpenFileWebJS : null); if (handler) { handler(textHTML); } } function AjaxJS(url, callback) { if (typeof window !== 'undefined') { let req = null; if (window.XMLHttpRequest) { req = new XMLHttpRequest(); req.onreadystatechange = function () { if (req.readyState === 4) { if (req.status === 200) { const textHTML = req.responseText; openResponse(textHTML, callback); } else { openResponse('', callback); } } }; req.open('GET', url, true); req.send(null); return req; } if (typeof window.ActiveXObject !== 'undefined') { try { req = new ActiveXObject('Msxml2.XMLHTTP.4.0'); } catch (e) { try { req = new ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch (e2) { try { req = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e3) { try { req = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e4) { req = false; } } } } if (req) { req.onreadystatechange = function () { if (req.readyState === 4) { if (req.status === 200) { const textHTML = req.responseText; openResponse(textHTML, callback); } else { openResponse('', callback); } } }; req.open('GET', url, true); req.send(); return req; } } } if (!fs) { throw new Error('File access is unavailable in this environment.'); } try { const textHTML = fs.readFileSync(url, 'utf8'); if (typeof callback === 'function') { callback(textHTML); } return textHTML; } catch (err) { if (typeof callback === 'function') { callback(''); } console.error(err.message); return ''; } } AjaxJS("dados.xml", function (textHTML) { if (!textHTML) { console.error('Não foi possível carregar dados.xml.'); return; } const xmlData = new DOMParser().parseFromString(textHTML, 'application/xml'); if (xmlData.querySelector('parsererror')) { console.error('O arquivo dados.xml contém XML inválido.'); return; } const alunos = Array.from(xmlData.getElementsByTagName('aluno')); const obterNome = aluno => aluno.getElementsByTagName('nome')[0].textContent; const obterNotas = aluno => Array.from(aluno.getElementsByTagName('nota')); rPrintln(obterNome(alunos[0])); // Escreve o nome do primeiro aluno: "João". rPrintln(obterNotas(alunos[1])[3].textContent); // Escreve a nota da última prova da Maria. rPrintln(obterNotas(alunos[2])[0].textContent); // Escreve a nota da primeira prova do Pedro. for (let i = 0; i < alunos.length; i++) { rPrintln(obterNome(alunos[i])); } // escreve o nome de todos os alunos for (let i = 0; i < alunos.length; i++) { rPrintln(obterNome(alunos[i])); for (let j = 0; j < obterNotas(alunos[i]).length; j++) { rPrintln(obterNotas(alunos[i])[j].textContent); } } // escreve o nome e todas as notas de todos os alunos }); </script> </body> </html> |