I'm trying to collect data from a huge XML, the structure is the following:
<Ciutats>
<Ciutat>
<Nom>Torremolinos</Nom>
<Provincia>Malaga</Provincia>
<Dades>
<Dada dia='2015-01-18'>
<TempMax>10.4</TempMax>
<TempMin>7.0</TempMin>
<TempAve>8.7</TempAve>
<VMax>8.0</VMax>
<VRatxa>28.0</VRatxa>
<Prec>29.8</Prec>
</Dada>
<Dada dia='2015-01-19'>
<TempMax>13.1</TempMax>
<TempMin>8.7</TempMin>
<TempAve>10.9</TempAve>
<VMax>29.0</VMax>
<VRatxa>64.0</VRatxa>
<Prec>6.2</Prec>
</Dada>
....
</Ciutat>
<Ciutat>
<Nom>Valdemorillo</Nom>
<Provincia>Madrid</Provincia>
<Dades>
<Dada dia='2015-01-18'>
<TempMax>1.7</TempMax>
<TempMin>-2.2</TempMin>
<TempAve>-0.3</TempAve>
<VMax>5.0</VMax>
<VRatxa>13.0</VRatxa>
<Prec>1.2</Prec>
</Dada>
<Dada dia='2015-01-19'>
<TempMax>3.6</TempMax>
<TempMin>0.3</TempMin>
<TempAve>1.9</TempAve>
<VMax>41.0</VMax>
<VRatxa>69.0</VRatxa>
<Prec>4.0</Prec>
</Dada>
...
</Ciutat>
</Ciutat>
for every "Ciutat" you have "Nom" "Provincia" and "Dades" where you have 29 Objects for every city, the XML has 784 cities in total.
And here's my code to read the XML:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File file = new File(fileName);
Document document = builder.parse(file);
Element ct = document.getDocumentElement();
NodeList ciutatList = ct.getElementsByTagName("Ciutat");
if (ciutatList != null && ciutatList.getLength() > 0) {
for (int i = 0; i < ciutatList.getLength(); i++) {
Element ciutats = (Element) ciutatList.item(i);
NodeList nomList = ciutats.getElementsByTagName("Nom");
if (nomList != null && nomList.getLength() > 0) {
Element nomElement = (Element) nomList.item(0);
nom = nomElement.getFirstChild().getNodeValue();
}
NodeList provList = ciutats.getElementsByTagName("Provincia");
if (provList != null && provList.getLength() > 0) {
Element provElement = (Element) provList.item(0);
prov = provElement.getFirstChild().getNodeValue();
}
tc = new TempsCiutat(nom, prov, 1000);
NodeList dadesList = ct.getElementsByTagName("Dades");
Element dadesElement = (Element) dadesList.item(0);
NodeList dadaList = dadesElement.getElementsByTagName("Dada");
for (int a = 0; a < dadaList.getLength(); a++) {
Date data = null;
float tempMax = 0, tempMin = 0, tempAve = 0, VMax = 0, VRatxa = 0, prec = 0;
Element dades = (Element) dadaList.item(a);
String dataS = dades.getAttribute("dia");
data = df.parse(dataS);
NodeList tempMaxList = dades.getElementsByTagName("TempMax");
if (tempMaxList != null && tempMaxList.getLength() > 0) {
Element tempMaxElement = (Element) tempMaxList.item(0);
String tempMaxS = tempMaxElement.getFirstChild().getNodeValue();
tempMax = Float.parseFloat(tempMaxS);
}
NodeList tempMinList = dades.getElementsByTagName("TempMin");
if (tempMinList != null && tempMinList.getLength() > 0) {
Element tempMinElement = (Element) tempMinList.item(0);
String tempMinS = tempMinElement.getFirstChild().getNodeValue();
tempMin = Float.parseFloat(tempMinS);
}
NodeList tempAveList = dades.getElementsByTagName("TempAve");
if (tempAveList != null && tempAveList.getLength() > 0) {
Element tempAveElement = (Element) tempAveList.item(0);
String tempAveS = tempAveElement.getFirstChild().getNodeValue();
tempAve = Float.parseFloat(tempAveS);
}
NodeList VmaxList = dades.getElementsByTagName("VMax");
if (VmaxList != null && VmaxList.getLength() > 0) {
Element VmaxElement = (Element) VmaxList.item(0);
String VMaxS = VmaxElement.getFirstChild().getNodeValue();
VMax = Float.parseFloat(VMaxS);
}
NodeList VRatxaList = dades.getElementsByTagName("VRatxa");
if (VRatxaList != null && VRatxaList.getLength() > 0) {
Element VRatxaElement = (Element) VRatxaList.item(0);
String VRatxaS = VRatxaElement.getFirstChild().getNodeValue();
VRatxa = Float.parseFloat(VRatxaS);
}
NodeList precList = dades.getElementsByTagName("Prec");
if (precList != null && precList.getLength() > 0) {
Element precElement = (Element) precList.item(0);
String precS = precElement.getFirstChild().getNodeValue();
prec = Float.parseFloat(precS);
}
TempsDia dia = new TempsDia(data, tempMax, tempMin, tempAve, VMax, VRatxa, prec);
tc.afegirTempsDia(dia);
}
tp.afegirCiutat(tc);
}
}
Now the problem is that when I read the first city it does it right, but, when it comes to read the second, the name and "Provincia" are correct, but the "Dades" are the same as the first city, I wonder if I have to close the Dada read somehow.