JDOM

JDOM

JDOM
JDOM

Informations
Développé par Jason Hunter, Brett McLaughlin, et Rolf Lear[1]
Première version le 1 mars 2000, il y a 26 ans
Dernière version 2.0.4 (le 8 novembre 2012, il y a 13 ans)
Dépôt github.com/hunterhacker/jdomVoir et modifier les données sur Wikidata
Écrit en Java
Environnement Multiplate-forme
Type XML
Licence Licence Apache
Site web http://jdom.org

JDOM en LePUS3
Fabriques JDOM en LePUS3

JDOM (acronyme de l'anglais Java Document Object Model), est une bibliothèque open source pour manipulation des fichiers XML en Java. Elle intègre DOM et SAX, et supporte XPath et XSLT. Elle utilise des analyses syntaxiques externes pour construire les documents.

Exemples

Soit le fichier "magasin.xml" :

<magasin nom="magasin pour geeks" localisation="Tokyo, Japon">
  <ordinateur nom="iBook" prix="1200" />
  <manga nom="Dragon Ball vol 1" prix="9" />
</magasin>

Il est possible de parser le document en un arbre d'objets Java avec JDOM :

import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new FileInputStream("magasin.xml"));
Element root = doc.getRootElement();
System.out.println(root.getName()); // renvoie "magasin"
System.out.println(root.getAttributeValue("nom")); //  "magasin pour geeks"
System.out.println(root.getAttributeValue("localisation")); //  "Tokyo, Japon"
System.out.println(root.getChildren()); // java.util.List de deux objets

Pour créer l'objet document sans fichier ni données en entrée :

Element root = new Element("magasin"); // définit la racine comme : <magasin></magasin>
Document doc = new Document(root);

Réciproquement, on peut construire un arbre d'éléments générant un fichier XML :

Element root = new Element("magasin");
root.setAttribute("nom", "magasin pour geeks");
root.setAttribute("localisation", "Tokyo, Japon");
Element item1 = new Element("ordinateur");
item1.setAttribute("nom", "iBook");
item1.setAttribute("prix", "1200");
root.addContent(item1);
XMLOutputter outputter = new XMLOutputter();
outputter.output(new Document(root), new FileOutputStream ("magasin2.xml"));
// crée la même chose que magasin.xml à partir du Java

Notes et références

  1. « JDOM : Who We Are », sur jdom.org (consulté le ).

Liens externes

Sur les autres projets Wikimedia :

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.