 
			
				29/03/2015, 12:57
			
			
			     |  
        |     |    |    Fecha de Ingreso: julio-2010  
						Mensajes: 110
					  Antigüedad: 15 años, 3 meses Puntos: 3     |        |  
  |      Respuesta: Leer XML varios bucles.        Esta es el mejor lector que he podido hacer hasta el momento ya que lee todas las ciudades y lo más importante, lee todos los días. El problema es que lee los días al final y como podemos comprobar en "nom"+name, los lee despues de haber leido las diferentes ciudades o sea ahora lee así  C (Ciudad) D (Dia)>> C1,C2,C3,D1,D2,D3,D4,D5  cuando tendría que leer por ejemplo C1,D1,C2,D2,C3,D3,D4,D5.   
Alguna idea para solucionar este problema?   
Codigo:       
import java.io.File; 
import java.io.IOException; 
import java.text.DateFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Vector;   
import javax.xml.parsers.*;   
import org.w3c.dom.*; 
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 lecturerList2 = ed.getElementsByTagName("Dada"); 
		if(lecturerList2 != null && lecturerList2.getLength() > 0) { 
			for (int i2=0; i2<lecturerList2.getLength(); i2++){ 
				Element lecturer2 = (Element) lecturerList2.item(i2); 
				dia = lecturer2.getAttribute("dia"); 
				//Get a lecturer     
				//Get lecturer's name label 
				NodeList nameList = lecturer2.getElementsByTagName("TempMax"); 
				if(nameList != null && nameList.getLength() > 0) { 
					Element nameElement = (Element)nameList.item(0); 
					tempMax = Float.parseFloat(nameElement.getFirstChild().getNo  deValue());   
				} 
				//Get lecturer's name label   
				NodeList provinceList = lecturer2.getElementsByTagName("TempMin"); 
				if(provinceList != null && provinceList.getLength() > 0) { 
					Element nameElement = (Element)provinceList.item(0); 
					tempMin = Float.parseFloat(nameElement.getFirstChild().getNo  deValue());						 
				}   
				provinceList = lecturer2.getElementsByTagName("TempAve"); 
				if(provinceList != null && provinceList.getLength() > 0) { 
					Element nameElement = (Element)provinceList.item(0); 
					tempAve = Float.parseFloat(nameElement.getFirstChild().getNo  deValue());						 
				}   
				provinceList = lecturer2.getElementsByTagName("VMax"); 
				if(provinceList != null && provinceList.getLength() > 0) { 
					Element nameElement = (Element)provinceList.item(0); 
					vMax = Float.parseFloat(nameElement.getFirstChild().getNo  deValue());						 
				}   
				provinceList = lecturer2.getElementsByTagName("VRatxa"); 
				if(provinceList != null && provinceList.getLength() > 0) { 
					Element nameElement = (Element)provinceList.item(0); 
					vRatxa = Float.parseFloat(nameElement.getFirstChild().getNo  deValue());						 
				}   
				provinceList = lecturer2.getElementsByTagName("Prec"); 
				if(provinceList != null && provinceList.getLength() > 0) { 
					Element nameElement = (Element)provinceList.item(0); 
					prec = Float.parseFloat(nameElement.getFirstChild().getNo  deValue());						 
				} 
				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);   
			}       
		} 
	} 
}   
}         
					
						Última edición por veniwarez; 29/03/2015 a las 13:13           |