Ver Mensaje Individual
  #10 (permalink)  
Antiguo 01/04/2015, 12:56
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.

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 original
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.text.ParseException;
  4.  
  5. import javax.xml.parsers.DocumentBuilder;
  6. import javax.xml.parsers.DocumentBuilderFactory;
  7. import javax.xml.parsers.ParserConfigurationException;
  8.  
  9. import org.w3c.dom.Document;
  10. import org.w3c.dom.Element;
  11. import org.w3c.dom.NodeList;
  12. import org.xml.sax.SAXException;
  13.  
  14. public class main {
  15.  
  16.     /**
  17.      * Run the examples of use org.apache.commons.cli for arguments and read XML
  18.      * file the construct the object {@link Lecturer}
  19.      *
  20.      * @param args
  21.      *            -h | -f path/of/file.xml
  22.      * @throws ParseException
  23.      * @throws IOException
  24.      * @throws ParserConfigurationException
  25.      * @throws SAXException
  26.      */
  27.     public static void main(String[] args) throws ParseException, IOException,
  28.             ParserConfigurationException, SAXException {
  29.         inicialitzar();
  30.  
  31.     }
  32.  
  33.     public static void inicialitzar() throws ParseException, IOException,
  34.             ParserConfigurationException, SAXException {
  35.         String fileName = "xml/dades2.xml";
  36.         /**
  37.          * Read XML file
  38.          */
  39.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  40.         DocumentBuilder builder = factory.newDocumentBuilder();
  41.         File file = new File(fileName);
  42.         Document document = builder.parse(file);
  43.  
  44.         // Return ed element of XML file.
  45.         Element ed = document.getDocumentElement();
  46.         String name = "";
  47.         String province = "";
  48.         // Return lecturer elements list
  49.         NodeList lecturerList = ed.getElementsByTagName("Ciutat");
  50.         if (lecturerList != null && lecturerList.getLength() > 0) {
  51.             for (int i = 0; i < lecturerList.getLength(); i++) {
  52.  
  53.                 // Get a lecturer
  54.                 Element lecturer = (Element) lecturerList.item(i);
  55.  
  56.                 // Get lecturer's name label
  57.                 name = new String();
  58.                 NodeList nameList = lecturer.getElementsByTagName("Nom");
  59.                 if (nameList != null && nameList.getLength() > 0) {
  60.                     Element nameElement = (Element) nameList.item(0);
  61.                     name = nameElement.getFirstChild().getNodeValue();
  62.  
  63.                 }
  64.                 // Get lecturer's name label
  65.                 province = new String();
  66.                 NodeList provinceList = lecturer
  67.                         .getElementsByTagName("Provincia");
  68.                 if (provinceList != null && provinceList.getLength() > 0) {
  69.                     Element nameElement = (Element) provinceList.item(0);
  70.                     province = nameElement.getFirstChild().getNodeValue();
  71.  
  72.                 }
  73.                 // tco=new TempsCiutatOrdenat(name,province,maxim);
  74.                 System.out.println("----------------------------------");
  75.  
  76.                 System.out.println(" CITY: " + name);
  77.  
  78.                 System.out.println(" PROVINCE: " + province);
  79.                 System.out.println("----------------------------------");
  80.  
  81.                 String dia = "";
  82.                 float tempMax = 0F;
  83.                 float tempMin = 0F;
  84.                 float tempAve = 0F;
  85.                 float vMax = 0F;
  86.                 float vRatxa = 0F;
  87.                 float prec = 0F;
  88.                 NodeList dadesList = lecturer.getElementsByTagName("Dades");
  89.                 NodeList dadaList = ((Element) dadesList.item(0))
  90.                         .getElementsByTagName("Dada");
  91.                 for (int j = 0; j < dadaList.getLength(); j++) {
  92.                     Element lecturer2 = (Element) dadaList.item(j);
  93.                     dia = lecturer2.getAttribute("dia");
  94.                     // Get a lecturer
  95.  
  96.                     // Get lecturer's name label
  97.                     NodeList tempMaxList = lecturer2
  98.                             .getElementsByTagName("TempMax");
  99.                     if (tempMaxList != null && tempMaxList.getLength() > 0) {
  100.                         Element nameElement = (Element) tempMaxList.item(0);
  101.                         tempMax = Float.parseFloat(nameElement.getFirstChild()
  102.                                 .getNodeValue());
  103.  
  104.                     }
  105.                     // Get lecturer's name label
  106.  
  107.                     NodeList tempMinList = lecturer2
  108.                             .getElementsByTagName("TempMin");
  109.                     if (tempMinList != null && tempMinList.getLength() > 0) {
  110.                         Element nameElement = (Element) tempMinList.item(0);
  111.                         tempMin = Float.parseFloat(nameElement.getFirstChild()
  112.                                 .getNodeValue());
  113.                     }
  114.  
  115.                     NodeList tempAveList = lecturer2
  116.                             .getElementsByTagName("TempAve");
  117.                     if (tempAveList != null && tempAveList.getLength() > 0) {
  118.                         Element nameElement = (Element) tempAveList.item(0);
  119.                         tempAve = Float.parseFloat(nameElement.getFirstChild()
  120.                                 .getNodeValue());
  121.                     }
  122.  
  123.                     NodeList vMaxList = lecturer2.getElementsByTagName("VMax");
  124.                     if (vMaxList != null && vMaxList.getLength() > 0) {
  125.                         Element nameElement = (Element) vMaxList.item(0);
  126.                         vMax = Float.parseFloat(nameElement.getFirstChild()
  127.                                 .getNodeValue());
  128.                     }
  129.  
  130.                     NodeList vRatxaList = lecturer2
  131.                             .getElementsByTagName("VRatxa");
  132.                     if (vRatxaList != null && vRatxaList.getLength() > 0) {
  133.                         Element nameElement = (Element) vRatxaList.item(0);
  134.                         vRatxa = Float.parseFloat(nameElement.getFirstChild()
  135.                                 .getNodeValue());
  136.                     }
  137.  
  138.                     NodeList precList = lecturer2.getElementsByTagName("Prec");
  139.                     if (precList != null && precList.getLength() > 0) {
  140.                         Element nameElement = (Element) precList.item(0);
  141.                         prec = Float.parseFloat(nameElement.getFirstChild()
  142.                                 .getNodeValue());
  143.                     }
  144.                     /*
  145.                      * DateFormat d1 = new SimpleDateFormat("yyyy-MM-dd"); Date
  146.                      * data1 = d1.parse(dia);
  147.                      */
  148.                     // tco.afegirTempsDia(data1, tempMax, tempMin, tempAve,
  149.                     // vMax, vRatxa, prec);
  150.  
  151.                     System.out.println("**************************");
  152.                     System.out.println(" DIA: " + dia + " nom" + name);
  153.                     System.out.println(" TEMP MAX:" + tempMax);
  154.                     System.out.println(" TEMP MIN:" + tempMin);
  155.                     System.out.println(" TEMP AVE:" + tempAve);
  156.                     System.out.println(" TEMP VMAX:" + vMax);
  157.                     System.out.println(" TEMP VRATXA:" + vRatxa);
  158.                     System.out.println(" TEMP PREC:" + prec);
  159.                 }
  160.  
  161.             }
  162.         }
  163.     }
  164. }

Un saludo
__________________
If to err is human, then programmers are the most human of us