Example usage for org.jdom2.input SAXBuilder SAXBuilder

List of usage examples for org.jdom2.input SAXBuilder SAXBuilder

Introduction

In this page you can find the example usage for org.jdom2.input SAXBuilder SAXBuilder.

Prototype

public SAXBuilder() 

Source Link

Document

Creates a new JAXP-based SAXBuilder.

Usage

From source file:fr.ms.tomcat.manager.ContextFileUtils.java

License:Apache License

public static File creationFichierContextDocBase(final File fichierContext, final File webappDirectory)
        throws JDOMException, IOException {

    Element context = null;/*from  w  ww.  j  a  va 2s . com*/
    Document document = null;

    if (fichierContext.exists()) {
        LOG.debug("Le fichier context existe dj dans la webapps : {}.", fichierContext.getAbsolutePath());
        final SAXBuilder sxb = new SAXBuilder();
        document = sxb.build(fichierContext);
        context = document.getRootElement();
    } else {
        LOG.debug("Le fichier context n'existe pas dans la webapps : {}.", fichierContext.getAbsolutePath());
        context = new Element("Context");
        document = new Document(context);
    }

    final Attribute docBase = new Attribute("docBase", webappDirectory.getAbsolutePath());
    final Attribute workDir = new Attribute("workDir", webappDirectory.getAbsolutePath() + "-workDir");
    context.setAttribute(docBase);
    context.setAttribute(workDir);

    final XMLOutputter xmlContextFile = new XMLOutputter(Format.getPrettyFormat());

    if (LOG.isDebugEnabled()) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        xmlContextFile.output(document, baos);

        LOG.debug(baos.toString());
    }

    final File fichierContextTemporaire = File.createTempFile("contextTomcat", ".xml");

    final FileOutputStream fichierContextTemporaireOutputStream = new FileOutputStream(
            fichierContextTemporaire);

    xmlContextFile.output(document, fichierContextTemporaireOutputStream);
    fichierContextTemporaireOutputStream.close();
    return fichierContextTemporaire;
}

From source file:fr.rt.acy.locapic.gps.TrackService.java

License:Open Source License

/**
 * Methode qui ajoute la localisation a partir de l'element JDOM passe en parametre
 * Utilise les attributs de classe FILE_DIRECTORY et FILE_NAME comme chemin pour le fichier a reecrire
 * @param loc - Element JDOM representant la nouvelle position
 * @return true//from w w  w . j  ava2 s . c  om
 */
public boolean addLocation(Element loc) {
    // Parsing
    SAXBuilder saxBuilder = new SAXBuilder();
    Document document = new Document(new Element("temp"));
    try {
        // On creer un nouveau document JDOM a partir du fichier GPX
        if (!FILE_EXT)
            document = saxBuilder.build(openFileInput(FILE_NAME));
        else
            document = saxBuilder.build(new FileInputStream(new File(EXT_FILES_DIR + FILE_NAME)));

        // On recupere la racine du document
        Element racine = document.getRootElement();
        // A partir de la racine, on recupere les tracks (<trk>)
        List<Element> trkList = racine.getChildren("trk", ns);
        // A partir de la derniere track, on recupere les segments de track (<trkseg>)
        List<Element> trksegList = trkList.get(trkList.size() - 1).getChildren("trkseg", ns);
        // On recupere le dernier segment de track (de la derniere track donc)
        Element trkseg = trksegList.get(trksegList.size() - 1);
        // On y ajoute la nouvelle position (Element loc)
        trkseg.addContent(loc);
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
    // Partie enregistrement dans le Fichier
    XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
    //Log.d(TAG, "addLocation : FILE_EXT = "+FILE_EXT);
    try {
        if (!FILE_EXT)
            xmlOutput.output(document, openFileOutput(FILE_NAME, MODE_WORLD_READABLE));
        else
            xmlOutput.output(document, new FileOutputStream(EXT_FILES_DIR + FILE_NAME));
    } catch (FileNotFoundException e) {
        Log.e(TAG, e.toString());
    } catch (IOException ioe) {
        Log.e(TAG, ioe.toString());
    }

    return true;
}

From source file:Frames.CrExperience2.java

private int creerConfig() throws DataConversionException {
    int x = 0;/*from w  ww .  j  av a  2  s  .co m*/
    File f = new File("Exp2/Experiences.xml");
    if (f.exists()) {
        System.out.println("Experiences Existe");
        SAXBuilder sxb = new SAXBuilder();
        try {

            document = sxb.build(new File("Exp2/Experiences.xml"));
        } catch (Exception e) {
        }
        racine = document.getRootElement();
        if (!racine.getChildren("experience").isEmpty()) {
            List<Element> childs = racine.getChildren("experience");

            x = childs.get(childs.size() - 1).getAttribute("id").getIntValue();

        }

    } else {
        try {
            //On utilise ici un affichage classique avec getPrettyFormat()
            XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());

            //Remarquez qu'il suffit simplement de crer une instance de FileOutputStream
            //avec en argument le nom du fichier pour effectuer la srialisation.
            sortie.output(document, new FileOutputStream("Exp2/Experiences.xml"));
        } catch (IOException ex) {

        }
    }
    return x;
}

From source file:Frames.CrExperience3.java

private int creerConfig() throws DataConversionException {
    int x = 0;/*w  w  w .j  a va 2s .  co m*/
    File f = new File("Exp3/Experiences.xml");
    if (f.exists()) {
        System.out.println("Experiences Existe");
        SAXBuilder sxb = new SAXBuilder();
        try {

            document = sxb.build(new File("Exp3/Experiences.xml"));
        } catch (Exception e) {
        }
        racine = document.getRootElement();
        if (!racine.getChildren("experience").isEmpty()) {
            List<Element> childs = racine.getChildren("experience");

            x = childs.get(childs.size() - 1).getAttribute("id").getIntValue();

        }

    } else {
        try {
            //On utilise ici un affichage classique avec getPrettyFormat()
            XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());

            //Remarquez qu'il suffit simplement de crer une instance de FileOutputStream
            //avec en argument le nom du fichier pour effectuer la srialisation.
            sortie.output(document, new FileOutputStream("Exp3/Experiences.xml"));
        } catch (IOException ex) {

        }
    }
    return x;
}

From source file:geosolutions.zoom.system.XMLConfiguration.java

public boolean readXmlParametrosSystem(String path) {
    File file = new File(path + "/config.xml");
    SAXBuilder sAXBuilder = new SAXBuilder();
    if (file.exists()) {
        Cripto cripto = new Cripto();
        try {/*from  ww w .  ja v a2s. c om*/
            Document documento = sAXBuilder.build(file);
            Element root = documento.getRootElement();
            /**------------------servidor---------------------------*/
            Element nodoServidor = root.getChild("servidor");
            //hijos
            ParametrosSystem.setDriverPostgres(nodoServidor.getChild("driver").getText());
            ParametrosSystem.setUrlPostgres(nodoServidor.getChild("url").getText());
            ParametrosSystem.setIpServidor(nodoServidor.getChild("ip_servidor").getText());
            ParametrosSystem.setBaseDatos(nodoServidor.getChild("db").getText());
            ParametrosSystem.setNombrePcServidor(nodoServidor.getChild("nombre_servidor").getText());
            ParametrosSystem.setPuertoPostgres(nodoServidor.getChild("puerto").getText());
            ParametrosSystem.setUsuarioPostgres(nodoServidor.getChild("usuario").getText());
            ParametrosSystem.setPasswordPostgres(nodoServidor.getChild("password").getText());
            ParametrosSystem.setNickSistema(nodoServidor.getChild("nick").getText());

            /**------------------ciente---------------------------*/
            Element nodoCliente = root.getChild("cliente");
            //hijos
            ParametrosSystem.setIpCliente(nodoCliente.getChild("ip_cliente").getText());
            ParametrosSystem.setNombrePcCliente(nodoCliente.getChild("nombre_cliente").getText());

            /**------------------otro servidor---------------------------*/
            Element nodoOtrosServidor = root.getChild("otro_servidor");
            //hijos
            ParametrosSystem.setOtroIpServidor(nodoOtrosServidor.getChild("otro_ip_servidor").getText());
            ParametrosSystem.setOtroPuertoPostgres(nodoOtrosServidor.getChild("otro_puerto").getText());
            ParametrosSystem.setOtroDB(nodoOtrosServidor.getChild("otro_db").getText());

            /**------------------otros---------------------------*/
            Element nodoOtros = root.getChild("otros");
            //hijos
            ParametrosSystem.setEsServidor(nodoOtros.getChild("es_servidor").getText());
            ParametrosSystem.setInterrupcion(nodoOtros.getChild("interrupcion").getText());
            ParametrosSystem.setHoraCierrePlanilla(nodoOtros.getChild("hora_cierre_planilla").getText());
            ParametrosSystem.setEsAutorizado(nodoOtros.getChild("es_autorizado_backup").getText());
            /**------------------otros---------------------------*/
            Element nodoPath = root.getChild("path");
            //hijos
            ParametrosSystem.setPathReport(nodoPath.getChild("path_reporte").getText());
            ParametrosSystem.setPathPgDumpPostgres(nodoPath.getChild("path_pg_dump").getText());
            ParametrosSystem.setPathBackup(nodoPath.getChild("path_backup").getText());

            return true;
        } catch (JDOMException ex) {
            Logger.getLogger(XMLConfiguration.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (IOException ex) {
            Logger.getLogger(XMLConfiguration.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    } else {
        return false;
    }
}

From source file:gestetu05.Client.java

static void LireXML(String NomFichier) {
    Element racine = new Element("repertoire");
    Document document = new Document(racine);

    //On cre une instance de SAXBuilder
    SAXBuilder sxb = new SAXBuilder();
    try {//  ww  w.ja v  a  2  s .  co  m
        //On cre un nouveau document JDOM avec en argument le fichier XML
        //Le parsing est termin ;)
        document = sxb.build(new File(NomFichier));
    } catch (JDOMException | IOException e) {
    }

    //On initialise un nouvel lment racine avec l'lment racine du document.
    racine = document.getRootElement();
    //On cre une List contenant tous les noeuds "utilisateur" de l'Element racine
    List listUtilisateurs = racine.getChildren("utilisateur");
    //On cre un Iterator sur notre liste
    Iterator i = listUtilisateurs.iterator();
    System.out.println("--------------------Fil d'actualit--------------------");
    while (i.hasNext()) {
        //On recre l'Element courant  chaque tour de boucle afin de
        //pouvoir utiliser les mthodes propres aux Element comme :
        //slectionner un nud fils, modifier du texte, etc...
        Element courant = (Element) i.next();
        //On affiche le nom de llment courant
        System.out.println("L'utilisateur " + courant.getChild("nom").getText());
        System.out.println(" est " + courant.getChild("Profession").getText());

    }
}

From source file:gestetu05.Client.java

static void ChercherInformation(String NomFichier, String recherche) {
    Element racine = new Element("repertoire");
    Document document = new Document(racine);
    //On cre une instance de SAXBuilder
    SAXBuilder sxb = new SAXBuilder();
    try {// w  ww  . j  a va  2s. co m
        //On cre un nouveau document JDOM avec en argument le fichier XML
        //Le parsing est termin ;)
        document = sxb.build(new File(NomFichier));
    } catch (JDOMException | IOException e) {
    }

    //On initialise un nouvel lment racine avec l'lment racine du document.
    racine = document.getRootElement();
    //On cre une List contenant tous les noeuds "utilisateur" de l'Element racine
    List listUtilisateurs = racine.getChildren("utilisateur");
    //On cre un Iterator sur notre liste
    Iterator i = listUtilisateurs.iterator();
    System.out.println("Rsultat de la recherche:\n");
    while (i.hasNext()) {
        //On recre l'Element courant  chaque tour de boucle afin de
        //pouvoir utiliser les mthodes propres aux Element comme :
        //slectionner un nud fils, modifier du texte, etc...
        Element courant = (Element) i.next();
        //On affiche le nom de llment courant
        if (courant.getChild("nom").getText().equals(recherche)
                || courant.getChild("Profession").getText().equals(recherche)) {
            System.out.println("Nom:" + courant.getChild("nom").getText());
            System.out.println("Profession:" + courant.getChild("Profession").getText() + "\n");
        }

    }
}

From source file:gestetu05.Client.java

public static Document readFromString(String xmlString) throws JDOMException, IOException {
    SAXBuilder builder = new SAXBuilder();
    return builder.build(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
}

From source file:gestetu05.GestionnaireUtilisateur.java

void ChercherInformation(String NomFichier, String nom, String mdp) {

    //On cre une instance de SAXBuilder
    SAXBuilder sxb = new SAXBuilder();
    try {// w  w w. j  a v  a 2s  . co m
        //On cre un nouveau document JDOM avec en argument le fichier XML
        //Le parsing est termin ;)
        document = sxb.build(new File(NomFichier));
    } catch (JDOMException | IOException e) {
    }

    //On initialise un nouvel lment racine avec l'lment racine du document.
    racine = document.getRootElement();
    //On cre une List contenant tous les noeuds "utilisateur" de l'Element racine
    List listUtilisateurs = racine.getChildren("utilisateur");
    //On cre un Iterator sur notre liste
    Iterator i = listUtilisateurs.iterator();

    while (i.hasNext()) {
        //On recre l'Element courant  chaque tour de boucle afin de
        //pouvoir utiliser les mthodes propres aux Element comme :
        //slectionner un nud fils, modifier du texte, etc...
        Element courant = (Element) i.next();
        //On affiche le nom de llment courant

        System.out.println(courant.getChild("nom").getText());
        System.out.println(courant.getChild("MotDePasse").getText());
        System.out.println("nom2" + nom);
        System.out.println("mdp2" + mdp);
        if (courant.getChild("nom").getText().equals(nom)
                && courant.getChild("MotDePasse").getText().equals(mdp)) {
            resultatRecherche = 1;
            System.out.println("good");
            break;
        } else {
            resultatRecherche = 0;
        }

    }
    System.out.println("_-_" + resultatRecherche);
}

From source file:gestetu05.GestionnaireUtilisateur.java

void ModicationXML(String NomFichier, String NomU, String MdpU) {
    List<String> ListeUtilisateur = new ArrayList<>();
    //On cre une instance de SAXBuilder
    SAXBuilder sxb = new SAXBuilder();
    try {/*from   w w  w  . j  av a 2  s  .  com*/
        //On cre un nouveau document JDOM avec en argument le fichier XML
        //Le parsing est termin ;)
        document = sxb.build(new File(NomFichier));
    } catch (JDOMException | IOException e) {
    }

    //On initialise un nouvel lment racine avec l'lment racine du document.
    racine = document.getRootElement();
    //On cre une List contenant tous les noeuds "utilisateur" de l'Element racine
    List listUtilisateurs = racine.getChildren("utilisateur");
    //On cre un Iterator sur notre liste
    Iterator i = listUtilisateurs.iterator();

    while (i.hasNext()) {
        //On recre l'Element courant  chaque tour de boucle afin de
        //pouvoir utiliser les mthodes propres aux Element comme :
        //slectionner un nud fils, modifier du texte, etc...
        Element courant = (Element) i.next();
        String U;
        String M;
        U = courant.getChild("nom").getText();
        M = courant.getChild("MotDePasse").getText();
        //On affiche le nom de llment courant
        if (M.equals(MdpU) && U.equals(NomU)) {

            System.out.println("Utilisateur trouv");

        } else {
            ListeUtilisateur.add(courant.getChild("nom").getText());
            ListeUtilisateur.add(courant.getChild("MotDePasse").getText());
            ListeUtilisateur.add(courant.getChild("Profession").getText());
        }

    }
    System.out.println("Liste modifie:" + ListeUtilisateur);
    EcrireFichierXML(ListeUtilisateur);
    enregistreXML("Exercice.xml");
}