Aniadido importar XML
This commit is contained in:
		
							parent
							
								
									1e46c48068
								
							
						
					
					
						commit
						78837d32b3
					
				
							
								
								
									
										4
									
								
								.config
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								.config
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,4 @@ | ||||
| #Configuracion general | ||||
| #Sat Nov 16 23:04:44 CET 2019 | ||||
| tema=0 | ||||
| ruta=.mes | ||||
							
								
								
									
										1
									
								
								gestiones.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								gestiones.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | ||||
| <?xml version="1.0" encoding="UTF-8" standalone="no"?><xml><raiz><mes><nombre>NOVEMBER</nombre><anio>2019</anio><dineroRestante>-1.0</dineroRestante><numeroGestiones>0</numeroGestiones><Gestiones><gestion><Nombre>Ingresos</Nombre><Dinero>4.0</Dinero><EsPositivo>true</EsPositivo><Transacciones><transaccion><Nombre>zfgabbaf</Nombre><Fecha>2019-11-16</Fecha><Dinero>4.0</Dinero></transaccion></Transacciones></gestion><gestion><Nombre>Gastos</Nombre><Dinero>5.0</Dinero><EsPositivo>false</EsPositivo><Transacciones><transaccion><Nombre>fdfsba</Nombre><Fecha>2019-11-16</Fecha><Dinero>5.0</Dinero></transaccion></Transacciones></gestion></Gestiones></mes></raiz></xml> | ||||
| @ -9,8 +9,10 @@ import java.io.IOException; | ||||
| import java.io.ObjectInputStream; | ||||
| import java.io.ObjectOutputStream; | ||||
| import java.io.Serializable; | ||||
| import java.time.LocalDate; | ||||
| import java.time.Month; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Locale; | ||||
| import java.util.Vector; | ||||
| 
 | ||||
| import javax.xml.parsers.DocumentBuilder; | ||||
| @ -29,7 +31,11 @@ import javax.xml.transform.stream.StreamResult; | ||||
| import org.w3c.dom.DOMImplementation; | ||||
| import org.w3c.dom.Document; | ||||
| import org.w3c.dom.Element; | ||||
| import org.w3c.dom.Node; | ||||
| import org.w3c.dom.NodeList; | ||||
| import org.w3c.dom.Text; | ||||
| import org.w3c.dom.traversal.NodeIterator; | ||||
| import org.xml.sax.SAXException; | ||||
| 
 | ||||
| public class Meses { | ||||
| 	private ArrayList<Mes> meses; | ||||
| @ -157,6 +163,8 @@ public class Meses { | ||||
| 			Element nodoGestion; | ||||
| 			Element nodoTransaccion; | ||||
| 			Element nodoDatos; | ||||
| 			Element nodoRaizGestion; | ||||
| 			Element nodoRaizTransaccion; | ||||
| 			Text dato; | ||||
| 			for(Mes mes:this.meses) { | ||||
| 				nodoMeses = documento.createElement("mes"); | ||||
| @ -177,9 +185,11 @@ public class Meses { | ||||
| 				nodoMeses.appendChild(nodoDatos); | ||||
| 				dato = documento.createTextNode(String.valueOf(mes.nGestiones)); | ||||
| 				nodoDatos.appendChild(dato); | ||||
| 				nodoRaizGestion = documento.createElement("Gestiones"); | ||||
| 				nodoMeses.appendChild(nodoRaizGestion); | ||||
| 				for(Gestion gestion:mes.gestiones) { | ||||
| 					nodoGestion = documento.createElement("gestion"); | ||||
| 					nodoMeses.appendChild(nodoGestion); | ||||
| 					nodoRaizGestion.appendChild(nodoGestion); | ||||
| 					nodoDatos = documento.createElement("Nombre"); | ||||
| 					nodoGestion.appendChild(nodoDatos); | ||||
| 					dato = documento.createTextNode(gestion.getNombre()); | ||||
| @ -192,21 +202,23 @@ public class Meses { | ||||
| 					nodoGestion.appendChild(nodoDatos); | ||||
| 					dato = documento.createTextNode(String.valueOf(gestion.esIngreso())); | ||||
| 					nodoDatos.appendChild(dato); | ||||
| 					nodoRaizTransaccion = documento.createElement("Transacciones"); | ||||
| 					nodoGestion.appendChild(nodoRaizTransaccion); | ||||
| 					for(Transaccion transaccion:gestion.getElementos()) { | ||||
| 						nodoTransaccion = documento.createElement("transaccion"); | ||||
| 						nodoGestion.appendChild(nodoTransaccion); | ||||
| 						nodoRaizTransaccion.appendChild(nodoTransaccion); | ||||
| 						nodoDatos = documento.createElement("Nombre"); | ||||
| 						nodoTransaccion.appendChild(nodoDatos); | ||||
| 						dato = documento.createTextNode(transaccion.getNombre()); | ||||
| 						nodoTransaccion.appendChild(dato); | ||||
| 						nodoDatos.appendChild(dato); | ||||
| 						nodoDatos = documento.createElement("Fecha"); | ||||
| 						nodoTransaccion.appendChild(nodoDatos); | ||||
| 						dato = documento.createTextNode(transaccion.getDia().toString()); | ||||
| 						nodoTransaccion.appendChild(dato); | ||||
| 						nodoDatos.appendChild(dato); | ||||
| 						nodoDatos = documento.createElement("Dinero"); | ||||
| 						nodoTransaccion.appendChild(nodoDatos); | ||||
| 						dato = documento.createTextNode(String.valueOf(transaccion.getDinero())); | ||||
| 						nodoTransaccion.appendChild(dato); | ||||
| 						nodoDatos.appendChild(dato); | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| @ -233,6 +245,57 @@ public class Meses { | ||||
| 		 | ||||
| 	} | ||||
| 	 | ||||
| 	public void importarXML() { | ||||
| 		this.importarXML("gestiones.xml"); | ||||
| 	} | ||||
| 	 | ||||
| 	public void importarXML(String nombreFichero) { | ||||
| 		File fichero = new File(nombreFichero); | ||||
| 		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||||
| 		try { | ||||
| 			DocumentBuilder builder = factory.newDocumentBuilder(); | ||||
| 			Document documento = builder.parse(fichero); | ||||
| 			NodeList gestiones = documento.getElementsByTagName("gestion"); | ||||
| 			//NodeList transacciones = documento.getElementsByTagName("transaccion"); | ||||
| 			NodeList meses = documento.getElementsByTagName("mes"); | ||||
| 			for(int h = 0; h < meses.getLength(); h++) { | ||||
| 				Node mesNodo = meses.item(h); | ||||
| 				Element elementoMes = (Element) mesNodo; | ||||
| 				for(int i = 0; i < gestiones.getLength();i++) { | ||||
| 					Node gestionNodo = gestiones.item(i); | ||||
| 					Element elementoGestion = (Element) gestionNodo; | ||||
| 					 | ||||
| 					Gestion gestion = this.aniadirGestion(elementoGestion.getElementsByTagName("Nombre").item(0).getChildNodes().item(0).getNodeValue(),  | ||||
| 							Integer.parseInt(elementoMes.getElementsByTagName("anio").item(0).getChildNodes().item(0).getNodeValue()), | ||||
| 							Month.valueOf(elementoMes.getElementsByTagName("nombre").item(0).getChildNodes().item(0).getNodeValue()), | ||||
| 							Boolean.parseBoolean(elementoGestion.getElementsByTagName("EsPositivo").item(0).getChildNodes().item(0).getNodeValue())); | ||||
| 					NodeList transacciones = elementoGestion.getElementsByTagName("transaccion"); | ||||
| 					for(int j=0; j < transacciones.getLength(); j++) { | ||||
| 						Node transaccio = transacciones.item(j); | ||||
| 						Element elementoTransaccion = (Element) transaccio; | ||||
| 						//System.out.println(elementoTransaccion.getElementsByTagName("Fecha").item(0).getChildNodes().item(0).getNodeValue()); | ||||
| 						String[] fecha = elementoTransaccion.getElementsByTagName("Fecha").item(0).getChildNodes().item(0).getNodeValue().split("-"); | ||||
| 						this.aniadirTransaccion(new Transaccion(elementoTransaccion.getElementsByTagName("Nombre").item(0).getChildNodes().item(0).getNodeValue(), | ||||
| 								Float.parseFloat(elementoTransaccion.getElementsByTagName("Dinero").item(0).getChildNodes().item(0).getNodeValue()), | ||||
| 								LocalDate.of(Integer.parseInt(fecha[0]), Integer.parseInt(fecha[1]), Integer.parseInt(fecha[2])), | ||||
| 								gestion), | ||||
| 								gestion.getNombre(), true); | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 			 | ||||
| 		} catch (ParserConfigurationException e) { | ||||
| 			// TODO Auto-generated catch block | ||||
| 			e.printStackTrace(); | ||||
| 		} catch (SAXException e) { | ||||
| 			// TODO Auto-generated catch block | ||||
| 			e.printStackTrace(); | ||||
| 		} catch (IOException e) { | ||||
| 			// TODO Auto-generated catch block | ||||
| 			e.printStackTrace(); | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
| } | ||||
| 
 | ||||
| class Mes implements Serializable{ | ||||
|  | ||||
| @ -11,6 +11,7 @@ public class BarraOpciones extends JMenuBar{ | ||||
| 	JMenuItem cambiarRuta; | ||||
| 	JMenuItem guardarIns; | ||||
| 	JMenuItem exportarXML; | ||||
| 	JMenuItem importarXML; | ||||
| 	JMenu interfaz; | ||||
| 	JMenuItem modoOscuro; | ||||
| 	JMenuItem modoRosa; | ||||
| @ -26,6 +27,8 @@ public class BarraOpciones extends JMenuBar{ | ||||
| 		this.menuAr.add(this.cambiarRuta); | ||||
| 		this.exportarXML = new JMenuItem("Exportar en un XML"); | ||||
| 		this.menuAr.add(this.exportarXML); | ||||
| 		this.importarXML = new JMenuItem("Importar en un XML"); | ||||
| 		this.menuAr.add(this.importarXML); | ||||
| 		this.interfaz = new JMenu("Interfaz"); | ||||
| 		this.add(this.interfaz); | ||||
| 		this.modoClaro = new JMenuItem("Modo claro"); | ||||
|  | ||||
| @ -3,6 +3,7 @@ package VistaControlador; | ||||
| import java.awt.event.ActionEvent; | ||||
| import java.awt.event.ActionListener; | ||||
| import java.io.IOException; | ||||
| import java.time.LocalDate; | ||||
| 
 | ||||
| import javax.swing.JOptionPane; | ||||
| 
 | ||||
| @ -65,6 +66,13 @@ public class ControladorBarra implements ActionListener{ | ||||
| 			} | ||||
| 			case "Exportar XLM":{ | ||||
| 				this.menu.meses.exportarXML(); | ||||
| 				break; | ||||
| 			} | ||||
| 			case "Importar XLM":{ | ||||
| 				this.menu.meses.importarXML(); | ||||
| 				this.menu.cargarGestiones(LocalDate.now().getYear(), LocalDate.now().getMonth()); | ||||
| 				this.menu.cargarPestanias(); | ||||
| 				break; | ||||
| 			} | ||||
| 		} | ||||
| 		 | ||||
| @ -77,6 +85,8 @@ public class ControladorBarra implements ActionListener{ | ||||
| 		this.barra.guardarIns.setActionCommand("Guardar instantanea"); | ||||
| 		this.barra.exportarXML.addActionListener(this); | ||||
| 		this.barra.exportarXML.setActionCommand("Exportar XLM"); | ||||
| 		this.barra.importarXML.addActionListener(this); | ||||
| 		this.barra.importarXML.setActionCommand("Importar XLM"); | ||||
| 		this.barra.modoClaro.addActionListener(this); | ||||
| 		this.barra.modoClaro.setActionCommand("Modo claro"); | ||||
| 		this.barra.modoRosa.addActionListener(this); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user