Buenas,
El código hace exactamente lo que le pides. Si no lo entiendes lo que estás programando, deberías empezar a estudiar lo más básico antes de empezar a hacer prueba/error.
Sin que sirva de precedente, lo he retocado para que te funcione. Intenta estudiarlo y entenderlo para que al menos el tiempo que le he dedicado sirva para algo:
Código Java:
Ver originalimport java.io.File;
import java.io.IOException;
import java.text.ParseException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class main {
/**
* Run the examples of use org.apache.commons.cli for arguments and read XML
* file the construct the object {@link Lecturer}
*
* @param args
* -h | -f path/of/file.xml
* @throws ParseException
* @throws IOException
* @throws ParserConfigurationException
* @throws SAXException
*/
ParserConfigurationException, SAXException {
inicialitzar();
}
ParserConfigurationException, SAXException {
String fileName
= "xml/dades2.xml"; /**
* Read XML file
*/
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document
= builder.
parse(file
);
// Return ed element of XML file.
Element ed
= document.
getDocumentElement(); // Return lecturer elements list
NodeList lecturerList = ed.getElementsByTagName("Ciutat");
if (lecturerList != null && lecturerList.getLength() > 0) {
for (int i = 0; i < lecturerList.getLength(); i++) {
// Get a lecturer
// Get lecturer's name label
NodeList nameList = lecturer.getElementsByTagName("Nom");
if (nameList != null && nameList.getLength() > 0) {
name = nameElement.getFirstChild().getNodeValue();
}
// Get lecturer's name label
NodeList provinceList = lecturer
.getElementsByTagName("Provincia");
if (provinceList != null && provinceList.getLength() > 0) {
province = nameElement.getFirstChild().getNodeValue();
}
// tco=new TempsCiutatOrdenat(name,province,maxim);
System.
out.
println("----------------------------------");
System.
out.
println(" CITY: " + name
);
System.
out.
println(" PROVINCE: " + province
); System.
out.
println("----------------------------------");
float tempMax = 0F;
float tempMin = 0F;
float tempAve = 0F;
float vMax = 0F;
float vRatxa = 0F;
float prec = 0F;
NodeList dadesList = lecturer.getElementsByTagName("Dades");
NodeList dadaList
= ((Element) dadesList.
item(0)) .getElementsByTagName("Dada");
for (int j = 0; j < dadaList.getLength(); j++) {
dia = lecturer2.getAttribute("dia");
// Get a lecturer
// Get lecturer's name label
NodeList tempMaxList = lecturer2
.getElementsByTagName("TempMax");
if (tempMaxList != null && tempMaxList.getLength() > 0) {
tempMax
= Float.
parseFloat(nameElement.
getFirstChild() .getNodeValue());
}
// Get lecturer's name label
NodeList tempMinList = lecturer2
.getElementsByTagName("TempMin");
if (tempMinList != null && tempMinList.getLength() > 0) {
tempMin
= Float.
parseFloat(nameElement.
getFirstChild() .getNodeValue());
}
NodeList tempAveList = lecturer2
.getElementsByTagName("TempAve");
if (tempAveList != null && tempAveList.getLength() > 0) {
tempAve
= Float.
parseFloat(nameElement.
getFirstChild() .getNodeValue());
}
NodeList vMaxList = lecturer2.getElementsByTagName("VMax");
if (vMaxList != null && vMaxList.getLength() > 0) {
vMax
= Float.
parseFloat(nameElement.
getFirstChild() .getNodeValue());
}
NodeList vRatxaList = lecturer2
.getElementsByTagName("VRatxa");
if (vRatxaList != null && vRatxaList.getLength() > 0) {
vRatxa
= Float.
parseFloat(nameElement.
getFirstChild() .getNodeValue());
}
NodeList precList = lecturer2.getElementsByTagName("Prec");
if (precList != null && precList.getLength() > 0) {
prec
= Float.
parseFloat(nameElement.
getFirstChild() .getNodeValue());
}
/*
* DateFormat d1 = new SimpleDateFormat("yyyy-MM-dd"); Date
* data1 = d1.parse(dia);
*/
// tco.afegirTempsDia(data1, tempMax, tempMin, tempAve,
// vMax, vRatxa, prec);
System.
out.
println("**************************"); System.
out.
println(" DIA: " + dia
+ " nom" + name
); System.
out.
println(" TEMP MAX:" + tempMax
); System.
out.
println(" TEMP MIN:" + tempMin
); System.
out.
println(" TEMP AVE:" + tempAve
); System.
out.
println(" TEMP VMAX:" + vMax
); System.
out.
println(" TEMP VRATXA:" + vRatxa
); System.
out.
println(" TEMP PREC:" + prec
); }
}
}
}
}
Un saludo