| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 
 |  
 
package ProjectPackage;
 
/**
 *
 * @author sousou
 */
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.filter.*;
import java.util.List;
import java.util.Iterator;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
 
 
public class ParserSysEquation {
 
    static Element racine = new Element("work");
 
    //On crée un nouveau Document JDOM basé sur la racine que l'on vient de créer
    static org.jdom.Document document = new Document(racine);
 
 
    public static void listChildren(Document d) {
 
        //On crée un nouvel Element cp et on l'ajoute
        //en temps qu'Element de racine
        Element cp = new Element("cp");
        racine.addContent(cp);
 
        //On crée un nouvel Element page, 
        //et on l'ajoute en temps qu'Element de cpnet
 
        Element page = new Element("page");
        cp.addContent(page);
 
        //On crée un nouvel Attribut classe et on l'ajoute à page
        //grâce à la méthode setAttribute
        Attribute idpage = new Attribute("id","ID6");
        page.setAttribute(idpage);
 
        Iterator i = d.getRootElement().getChildren().iterator();
        while (i.hasNext()) {
            String url;
 
            org.jdom.Element e = (Element)i.next(); 
 
            url = e.getAttributeValue("idvars");
 
            String idvars = e.getChildText("idvars");
            if (idvars==null) System.out.println("Pas de idvars");
            else System.out.println(""+idvars);
 
            Element place = new Element("place");
            page.addContent(place);
 
            Element idv= new Element("idv");
            // idv.setText(url);
            place.addContent(url);
 
            //Les deux méthodes qui suivent seront définies plus loin dans l'article
        }
 
    }
 
 
    public static void main(String[] args) {
 
        SAXBuilder builder = new SAXBuilder();
 
        try {
            Document doc = builder.build("sys.xml");
            listChildren(doc);
        }
        catch (JDOMException e) {
            System.out.println("sys.xml is not well-formed.");
            System.out.println(e.getMessage());
        }
        catch (IOException e) {
            System.out.println(e);
        }
 
    }
 
 
} | 
Partager