Ver Mensaje Individual
  #14 (permalink)  
Antiguo 02/04/2015, 03:57
Avatar de Profesor_Falken
Profesor_Falken
 
Fecha de Ingreso: agosto-2014
Ubicación: Mountain View
Mensajes: 1.323
Antigüedad: 9 años, 8 meses
Puntos: 182
Respuesta: Leer XML varios bucles.

Actualiza o prueba en otro navegador. Te deberia aparecer.

De cualquier forma te lo pego aqui sin hightlight:

import 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
*/
public static void main(String[] args) throws ParseException, IOException,
ParserConfigurationException, SAXException {
inicialitzar();

}

public static void inicialitzar() throws ParseException, IOException,
ParserConfigurationException, SAXException {
String fileName = "xml/dades2.xml";
/**
* Read XML file
*/
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File file = new File(fileName);
Document document = builder.parse(file);

// Return ed element of XML file.
Element ed = document.getDocumentElement();
String name = "";
String province = "";
// 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
Element lecturer = (Element) lecturerList.item(i);

// Get lecturer's name label
name = new String();
NodeList nameList = lecturer.getElementsByTagName("Nom");
if (nameList != null && nameList.getLength() > 0) {
Element nameElement = (Element) nameList.item(0);
name = nameElement.getFirstChild().getNodeValue();

}
// Get lecturer's name label
province = new String();
NodeList provinceList = lecturer
.getElementsByTagName("Provincia");
if (provinceList != null && provinceList.getLength() > 0) {
Element nameElement = (Element) provinceList.item(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("----------------------------------");

String dia = "";
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++) {
Element lecturer2 = (Element) dadaList.item(j);
dia = lecturer2.getAttribute("dia");
// Get a lecturer

// Get lecturer's name label
NodeList tempMaxList = lecturer2
.getElementsByTagName("TempMax");
if (tempMaxList != null && tempMaxList.getLength() > 0) {
Element nameElement = (Element) tempMaxList.item(0);
tempMax = Float.parseFloat(nameElement.getFirstChild()
.getNodeValue());

}
// Get lecturer's name label

NodeList tempMinList = lecturer2
.getElementsByTagName("TempMin");
if (tempMinList != null && tempMinList.getLength() > 0) {
Element nameElement = (Element) tempMinList.item(0);
tempMin = Float.parseFloat(nameElement.getFirstChild()
.getNodeValue());
}

NodeList tempAveList = lecturer2
.getElementsByTagName("TempAve");
if (tempAveList != null && tempAveList.getLength() > 0) {
Element nameElement = (Element) tempAveList.item(0);
tempAve = Float.parseFloat(nameElement.getFirstChild()
.getNodeValue());
}

NodeList vMaxList = lecturer2.getElementsByTagName("VMax");
if (vMaxList != null && vMaxList.getLength() > 0) {
Element nameElement = (Element) vMaxList.item(0);
vMax = Float.parseFloat(nameElement.getFirstChild()
.getNodeValue());
}

NodeList vRatxaList = lecturer2
.getElementsByTagName("VRatxa");
if (vRatxaList != null && vRatxaList.getLength() > 0) {
Element nameElement = (Element) vRatxaList.item(0);
vRatxa = Float.parseFloat(nameElement.getFirstChild()
.getNodeValue());
}

NodeList precList = lecturer2.getElementsByTagName("Prec");
if (precList != null && precList.getLength() > 0) {
Element nameElement = (Element) precList.item(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
__________________
If to err is human, then programmers are the most human of us