Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tanda1;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Iterator;
- import java.util.List;
- import org.jdom2.input.SAXBuilder;
- import org.jdom2.output.Format;
- import org.jdom2.output.XMLOutputter;
- import org.jdom2.*;
- public class PruebaVuelosNEREA {
- public static void main(String[] args) throws JDOMException, IOException {
- SAXBuilder builder=new SAXBuilder();
- Document doc=builder.build("vuelos.xml");
- Element raiz=doc.getRootElement();
- List hijos=raiz.getChildren("vuelo");
- //1
- System.out.println("Hay " + hijos.size() + " vuelos");
- //2
- System.out.println("Destinos desde Madrid:");
- Iterator it=hijos.iterator();
- while (it.hasNext()){
- Element v =(Element) it.next();
- // v.getChildText("origen").equals("Madrid")
- Element origen=v.getChild("origen");
- String txtorigen=origen.getText();
- if (txtorigen.equals("Madrid")){
- System.out.print(v.getChildText("destino")+" ");
- }
- }
- //3
- System.out.println("Introduce id-vuelo:");
- String idbuscado=Consola.leeString();
- boolean encontrado=false;
- Iterator it2=hijos.iterator();
- while (it2.hasNext() && !encontrado ){
- Element v =(Element) it2.next();
- String strid=v.getAttributeValue("id");//obtenemoes el valor del atributo
- if (idbuscado.equals(strid)){
- System.out.println( // mostramos del elemento el valor hijo del texto del origen destino y la hora
- v.getChildText("origen")+" " +
- v.getChildText("destino")+" " +
- v.getChildText("hora")
- );
- encontrado=true;//y damso el valor verdadero a la variabble enccontrado
- }
- }
- if (!encontrado)
- System.out.println("El vuelo con id "+ idbuscado + " no existe");
- //4
- System.out.println("\nVuelos posteriores a las 14:");
- for (int i=0; i<hijos.size(); i++){ //de la cantidad de hijos
- Element v=(Element) hijos.get(i);
- //obtenemos los elementos hijos de v
- String strhora=v.getChildText("hora");
- //obtenemos
- if (esPosterior14(strhora)==true){ // si la hora que obtenemos de la etiqueta hijo es verdadera
- System.out.println(
- v.getChildText("origen")+" " + // mostramos el texto de los atributos hijos
- v.getChildText("destino")+" "
- );
- }
- }
- //5
- System.out.print("Introduzca id vuelo:");
- String idv=Consola.leeString();
- System.out.print("Introduzca origen:");
- String or=Consola.leeString();
- System.out.print("Introduzca destino:");
- String des=Consola.leeString();
- System.out.print("Introduzca hora:");
- String hor=Consola.leeString();
- Element v=new Element("vuelo");
- Element origen=new Element("origen").addContent(or);
- Element destino=new Element("destino").setText(des);
- Element hora=new Element("hora").setText(hor);
- v.setAttribute("id",idv);
- v.addContent(origen);
- v.addContent(destino);
- v.addContent(hora);
- raiz.addContent(v);
- XMLOutputter salida= new XMLOutputter(Format.getPrettyFormat());
- salida.output(doc, new FileWriter("vuelos.xml"));
- //6
- Element v2 =(Element) hijos.get(hijos.size()-1 ); //eliminamos el contenido de el tamaño de los elementos
- // hijos obtenidos menos el ultimo que hayamos añadido
- raiz.removeContent(v2);// y nos encargaremos de borrar dicho contenido
- salida.output(doc, System.out);
- }
- private static boolean esPosterior14(String strhora) {
- int idp=strhora.indexOf(":");
- String strh =strhora.substring(0,idp);
- //obtener el substring de las horas entre 0 y el rango establecido entre los minutos
- String strm=strhora.substring(idp+1,strhora.length());
- //sacamos el rango de valores entra la hora y sus respectivos+1 y la longitud de dicha hora
- //String[] partes=strhora.split(":");
- //partes[0] tendrá la hora
- //partes[1] tendra los minutos
- int h=Integer.parseInt(strh); // posteriormente parseamos los valores a int
- int m=Integer.parseInt(strm);
- if (h>=14)
- return true;
- else
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment