Example usage for org.jdom2.input SAXBuilder build

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

Introduction

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

Prototype

@Override
public Document build(final String systemId) throws JDOMException, IOException 

Source Link

Document

This builds a document from the supplied URI.

Usage

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 2 s  .c  o 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 {// w  ww.java  2s  .  c  o  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();
        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");
}

From source file:gestetu05.GestionnaireUtilisateur.java

List<String> LireXML(String NomFichier) {
    List<String> ListeUtilisateur = new ArrayList<>();
    //On cre une instance de SAXBuilder
    SAXBuilder sxb = new SAXBuilder();
    try {// www  . j a va 2s .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();
        //On affiche le nom de llment courant
        ListeUtilisateur.add(courant.getChild("nom").getText());
        ListeUtilisateur.add(courant.getChild("MotDePasse").getText());
        ListeUtilisateur.add(courant.getChild("Profession").getText());

    }
    System.out.println(ListeUtilisateur);
    return ListeUtilisateur;
}

From source file:gestetu05.GestionnaireUtilisateur.java

int AjouterUtilisateur(List<String> AjoutUtilisateur, String NomFichier) {
    List<String> ListeUtilisateur = new ArrayList<>();
    ListeUtilisateur = LireXML(NomFichier);
    int resultatAjout = 0;

    //On cre une instance de SAXBuilder
    SAXBuilder sxb = new SAXBuilder();
    try {/*from  ww  w  .  j av  a2  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();

    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(AjoutUtilisateur.get(0))) {
            resultatAjout = 0;
            System.out.println("Utlisateur dj prsent");
            break;
        } else {
            resultatAjout = 1;
        }
    }

    if (resultatAjout == 1) {

        ListeUtilisateur.add(AjoutUtilisateur.get(0));
        ListeUtilisateur.add(AjoutUtilisateur.get(1));
        ListeUtilisateur.add(AjoutUtilisateur.get(2));
        System.out.println(ListeUtilisateur);
        EcrireFichierXML(ListeUtilisateur);
        enregistreXML("Exercice.xml");
    }
    return resultatAjout;
}

From source file:gestores.IOMapeadorURLs.java

public static MapeadorDeURLs importarMapeadorSAX(String URL) {
    MapeadorDeURLs mapeador = new MapeadorDeURLs();

    SAXBuilder builder = new SAXBuilder();

    File file = new File(URL);

    try {//from  w  w w  .ja  v a  2s . c  o m
        Document document = builder.build(file);

        Element rootNode = document.getRootElement();

        List<Element> lista = rootNode.getChildren(Configuracion.marcaMapeoMapeador);

        for (Element elemento : lista) {
            String URLMapeada = elemento.getChildText(Configuracion.marcaURLMapeador);
            int map = Integer.parseInt(elemento.getChildText(Configuracion.marcaMappingMapeador));

            mapeador.agregarURL(URLMapeada, map);
        }
    } catch (Exception e) {
        System.out.println("Error al intentar leer el mapeador desde la URL: " + URL);
        e.printStackTrace();
        return null;
    }

    return mapeador;
}

From source file:gestores.IOVocabularioGeneral.java

private static ArrayList<EntradaVocabularioGeneral> importarEntradasDeVocabularioSAX(String URL) {
    ArrayList<EntradaVocabularioGeneral> entradas = new ArrayList<>();

    SAXBuilder builder = new SAXBuilder();

    File file = new File(URL);

    try {/*w w  w. j av a  2 s  . com*/
        Document document = builder.build(file);

        Element rootNode = document.getRootElement();

        List<Element> lista = rootNode.getChildren(Configuracion.marcaEntradaVocabulario);

        for (Element elemento : lista) {

            EntradaVocabularioGeneral entrada = new EntradaVocabularioGeneral();
            entrada.setDocumentosEnQueAparece(
                    Integer.parseInt(elemento.getChildText(Configuracion.marcaCantidadDocumentosVocabulario)));
            entrada.setFrecuencia(
                    Integer.parseInt(elemento.getChildText(Configuracion.marcaFrecuenciaVocabulario)));
            entrada.setFrecuenciaMaxima(
                    Integer.parseInt(elemento.getChildText(Configuracion.marcaFrecuenciaMaximaVocabulario)));
            entrada.setPalabra((elemento.getChildText(Configuracion.marcaPalabraVocabulario)));

            entradas.add(entrada);
        }
    } catch (Exception e) {
        System.out.println("Error al intentar leer el vocabulario desde la URL: " + URL);
        e.printStackTrace();
        return new ArrayList<>();
    }

    return entradas;
}

From source file:gov.nasa.jpl.mudrod.main.MudrodEngine.java

License:Apache License

/**
 * Load the configuration provided at <a href=
 * "https://github.com/mudrod/mudrod/blob/master/core/src/main/resources/config.xml">config.xml</a>.
 *
 * @return a populated {@link java.util.Properties} object.
 *//*w w  w. j a  va2s . c  om*/
public Properties loadConfig() {
    SAXBuilder saxBuilder = new SAXBuilder();

    InputStream configStream = locateConfig();

    Document document;
    try {
        document = saxBuilder.build(configStream);
        Element rootNode = document.getRootElement();
        List<Element> paraList = rootNode.getChildren("para");

        for (int i = 0; i < paraList.size(); i++) {
            Element paraNode = paraList.get(i);
            String attributeName = paraNode.getAttributeValue("name");
            if (MudrodConstants.SVM_SGD_MODEL.equals(attributeName)) {
                props.put(attributeName, decompressSVMWithSGDModel(paraNode.getTextTrim()));
            } else {
                props.put(attributeName, paraNode.getTextTrim());
            }
        }
    } catch (JDOMException | IOException e) {
        LOG.error("Exception whilst retrieving or processing XML contained within 'config.xml'!", e);
    }
    return getConfig();

}

From source file:gov.nih.nci.restgen.codegen.RESTfulResourceGenerator.java

License:BSD License

private String generateMethodEJB(String resourceName, Method method, String methodName, StringTemplate template)
        throws GeneratorException {
    Implementation impl = method.getImplementation();

    template.setAttribute("MethodName", methodName);
    template.setAttribute("ResourceName", resourceName);

    JarFile jarFile = null;//from  w  w  w  .  ja  v a2s .co  m
    org.jdom2.Document doc = null;
    try {
        jarFile = new JarFile(context.getMapping().getOptions().getEjbLocation());
        JarEntry jarEntry = jarFile.getJarEntry("META-INF/ejb-jar.xml");
        if (jarEntry != null) {
            InputStream is = jarFile.getInputStream(jarEntry);
            SAXBuilder sax = new SAXBuilder();
            doc = sax.build(is);

        } else
            throw new GeneratorException("Invalid EJB JAR path. Unable to load ejb-jar.xml");
    } catch (IOException e) {
        throw new GeneratorException("Failed to load EJB JAR. ", e);
    } catch (JDOMException e) {
        throw new GeneratorException("Failed to load EJB JAR ejb-jar.xml ", e);
    }

    org.jdom2.Element root = doc.getRootElement();

    org.jdom2.Element eBeans = root.getChild("enterprise-beans", root.getNamespace());
    List<org.jdom2.Element> sessionBeans = eBeans.getChildren("session", root.getNamespace());
    String ejbHomeName = null;
    String ejbRemoteName = null;

    boolean foundService = false;
    for (org.jdom2.Element sessionBean : sessionBeans) {
        org.jdom2.Element ejbName = sessionBean.getChild("ejb-name", root.getNamespace());

        if (ejbName.getValue().equals(impl.getName())) {
            foundService = true;
            org.jdom2.Element ejbHome = sessionBean.getChild("home", root.getNamespace());
            ejbHomeName = ejbHome.getValue();
            org.jdom2.Element ejbRemote = sessionBean.getChild("remote", root.getNamespace());
            ejbRemoteName = ejbRemote.getValue();
            break;
        }
    }

    if (!foundService)
        throw new GeneratorException("Unable to find EJB from ejb-jar.xml for " + impl.getName());

    String returnType = getOperationReturnType(impl);
    if (!returnType.equals("void")) {
        template.setAttribute("ReturnTypeNotVoid", true);
        template.setAttribute("PostReturnType", returnType);
        template.setAttribute("PutReturnType", returnType);
        template.setAttribute("DeleteReturnType", returnType);
    } else {
        template.setAttribute("ReturnTypeResponse", true);
        template.setAttribute("PostReturnType", "Response");
        template.setAttribute("PutReturnType", "Response");
        template.setAttribute("DeleteReturnType", "Response");

    }

    template.setAttribute("ReturnType", returnType);
    String pathParamPath = getOperationPath(impl, method.getName());
    if (pathParamPath != null) {
        template.setAttribute("PathParamPath", "@Path(\"" + pathParamPath + "\")");
        template.setAttribute("PathParamPathShort", pathParamPath);
    }

    template.setAttribute("PathParam", getOperationPathParams(method, impl));
    template.setAttribute("HomeInterface", ejbHomeName);
    template.setAttribute("RemoteInterface", ejbRemoteName);
    template.setAttribute("OperationName", impl.getOperation().getName());
    String operationParams = getOperationParams(impl);
    //if (operationParams != null)
    //   operationParams = operationParams + ", ";
    template.setAttribute("OperationParameters", operationParams);
    template.setAttribute("RequestType", getRequestTypes(impl));
    template.setAttribute("OperationParamNames", getOperationParams(impl));

    if (impl.getClientType().equals(Implementation.EJB_REMOTE)) {
        String jndiPath = impl.getJndiProperties();
        String fileName = jndiPath.substring(jndiPath.lastIndexOf(File.separator) + 1);
        template.setAttribute("JNDIProperties", fileName);
    }
    template.setAttribute("JNDIName", impl.getJndiName());
    return template.toString();
}

From source file:gov.nih.nci.restgen.util.RESTContentHandler.java

License:BSD License

private <T> void marshallTypeCollection(List<T> value, Class<T> clzz, OutputStreamWriter os) {
    try {/*from w  w w .jav a  2  s. c  om*/
        if (value == null || value.size() == 0)
            return;

        Object obj = value.get(0);
        String className = obj.getClass().getName();
        String qName = obj.getClass().getName();
        if (clzz.getName().indexOf(".") > 0) {
            String temp = obj.getClass().getName().substring(obj.getClass().getName().lastIndexOf(".") + 1);
            qName = (temp.charAt(0) + "").toLowerCase() + temp.substring(1, temp.length());
        }
        String collectionName = className.substring(className.lastIndexOf(".") + 1) + "s";
        org.jdom2.Element httpQuery = new org.jdom2.Element(collectionName, "");

        Iterator iterator = value.iterator();
        JAXBContext jc = JAXBContext.newInstance(obj.getClass().getPackage().getName());
        Marshaller u = jc.createMarshaller();
        u.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
        StringWriter strWriter = null;
        Reader in = null;

        try {
            while (iterator.hasNext()) {
                Object objValue = iterator.next();
                strWriter = new StringWriter();
                u.marshal(new JAXBElement(new QName(qName), obj.getClass(), objValue), strWriter);
                in = new StringReader(strWriter.toString());
                org.jdom2.input.SAXBuilder builder = new org.jdom2.input.SAXBuilder();
                org.jdom2.Document doc = builder.build(in);
                org.jdom2.Element rootEle = (org.jdom2.Element) doc.getRootElement().clone();
                httpQuery.addContent(rootEle);
                strWriter.flush();
                in.close();
                strWriter.close();
            }
        } catch (JDOMException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        org.jdom2.Document xmlDoc = new org.jdom2.Document(httpQuery);
        org.jdom2.output.XMLOutputter outputter = new org.jdom2.output.XMLOutputter();
        try {
            outputter.output(xmlDoc, os);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } catch (JAXBException e) {
        e.printStackTrace();
    }
}