Skip to content

Instantly share code, notes, and snippets.

@magnunleno
Created October 25, 2012 17:10
Show Gist options
  • Save magnunleno/3954083 to your computer and use it in GitHub Desktop.
Save magnunleno/3954083 to your computer and use it in GitHub Desktop.
Processamento XML (Java vs. Python vs. C)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/parser.h>
#define CMP(name1, name2) xmlStrcmp(name1, (const xmlChar *)name2)
void parseAluno(xmlDocPtr doc, xmlNodePtr cur)
{
printf("-----------\n");
xmlChar *key;
for (cur=cur->xmlChildrenNode; cur!=NULL; cur=cur->next) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (!CMP(cur->name, "nome"))
printf("Nome: %s\n", key);
else if (!CMP(cur->name, "nota1"))
printf("Nota 1: %s\n", key);
else if (!CMP(cur->name, "nota2"))
printf("Nota 2: %s\n", key);
else if (!CMP(cur->name, "nota3"))
printf("Nota 3: %s\n", key);
else if (!CMP(cur->name, "nota4"))
printf("Nota 4: %s\n", key);
xmlFree(key);
}
}
int main(int argc, char const *argv[])
{
xmlDocPtr doc;
xmlNodePtr cur;
doc = xmlParseFile("teste.xml");
cur = xmlDocGetRootElement(doc);
for (cur=cur->xmlChildrenNode; cur!=NULL; cur=cur->next)
if (!CMP(cur->name, "aluno"))
parseAluno(doc, cur);
printf("----FIM----\n");
xmlFreeDoc(doc);
return 0;
}
import xml.etree.cElementTree as et
fd = open('teste.xml')
parsedXML = et.parse(fd)
alunos = parsedXML.findall('aluno')
for alunoNode in alunos:
aluno = dict((attr.tag, attr.text) for attr in alunoNode)
print '-----------'
print 'Nome:', aluno['nome']
print 'Nota 1:', aluno['nota1']
print 'Nota 2:', aluno['nota2']
print 'Nota 3:', aluno['nota3']
print 'Nota 4:', aluno['nota4']
print '----FIM----'
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.util.ArrayList;
import java.util.logging.Logger;
public class ProcessaApp {
public static void main(String[] args){
Document doc;
DocumentBuilder dBuilder;
DocumentBuilderFactory dbFactory;
NodeList nodeList;
NodeList alunos;
String valor;
try {
File fXmlFile = new File("teste.xml");
dbFactory = DocumentBuilderFactory.newInstance();
dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
} catch (Exception e) {
e.printStackTrace();
return;
}
alunos = doc.getElementsByTagName("aluno");
for (int alunoN = 0; alunoN < alunos.getLength(); alunoN++) {
Node nNode = alunos.item(alunoN);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("-----------");
// nome
nodeList = eElement.getElementsByTagName("nome");
valor = nodeList.item(0).getChildNodes().item(0).getNodeValue();
System.out.println("Nome: "+valor);
// nota1
nodeList = eElement.getElementsByTagName("nota1");
valor = nodeList.item(0).getChildNodes().item(0).getNodeValue();
System.out.println("Nota 1: "+valor);
// nota2
nodeList = eElement.getElementsByTagName("nota2");
valor = nodeList.item(0).getChildNodes().item(0).getNodeValue();
System.out.println("Nota 2: "+valor);
// nota3
nodeList = eElement.getElementsByTagName("nota3");
valor = nodeList.item(0).getChildNodes().item(0).getNodeValue();
System.out.println("Nota 3: "+valor);
// nota4
nodeList = eElement.getElementsByTagName("nota4");
valor = nodeList.item(0).getChildNodes().item(0).getNodeValue();
System.out.println("Nota 4: "+valor);
}
}
System.out.println("----FIM----");
}
}
<alunos>
<aluno>
<nome>Joao</nome>
<nota1>7</nota1>
<nota2>8</nota2>
<nota3>5</nota3>
<nota4>10</nota4>
</aluno>
<aluno>
<nome>Maria</nome>
<nota1>8</nota1>
<nota2>6</nota2>
<nota3>6</nota3>
<nota4>9</nota4>
</aluno>
<aluno>
<nome>Jose</nome>
<nota1>5</nota1>
<nota2>6</nota2>
<nota3>4</nota3>
<nota4>5</nota4>
</aluno>
</alunos>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment