1 2 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| public static class TraitementJdom {
Document document;
public static Document testJdom(String fileName, String name,String type, String lbl)
{
XMLOutputter outp = new XMLOutputter();
Document document = null;
File file = new File(fileName);
SAXBuilder builder = new SAXBuilder();
try
{
document = builder.build(file);
Element root = document.getRootElement();
System.out.println("<"+root.getName()+">"); // <---------------------- Début Racine
List<Element> children = root.getChildren();
for (Element c : children) {
System.out.println(" <"+c.getName()+">"); // <---------------------- Début Noeud
List<Element> child = c.getChildren();
for (Element e : child)
{
List<Element> c2 = new ArrayList<>(e.getChildren()); // on copie la liste : on pourra la parcourir en modifiant l'original sans problème
if (e.getName().equals(type)) {
addElementIn(e,lbl,name);
}
else
{
System.out.print(" <"+e.getName()+">"); // <------------------ Début Sous-Noeud
System.out.print(e.getText()) ;
System.out.println("</"+e.getName()+">"); // <------------------ Fin Sous-Noeud
}
}
System.out.println(" </"+c.getName()+">"); // <---------------------- Fin Noeud
//Extraire les donnees (attributes, values ...) ou parcourir en
// profondeur en fonction de child (noeud terminal ou arbre ....)
}
System.out.println("</"+root.getName()+">"); // <---------------------- Fin Racine
}
catch (JDOMException e)
{
System.err.println(e.getMessage());
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
return document;
}
public static Document testJdom1(String fileName, String name,String type, String lbl)
{
XMLOutputter outp = new XMLOutputter();
Document document = null;
File file = new File(fileName);
SAXBuilder builder = new SAXBuilder();
try
{
document = builder.build(file);
Element root = document.getRootElement();
System.out.println("<"+root.getName()+">"); // <---------------------- Début Racine
List<Element> children = root.getChildren();
for (Element c : children) {
System.out.println(" <"+c.getName()+">"); // <---------------------- Début Noeud
List<Element> child = c.getChildren();
List<Element> c2 = new ArrayList<>(c.getChildren()); // on copie la liste : on pourra la parcourir en modifiant l'original sans problème
if (c.getName().equals(type)) {
addElementAfter(c,lbl,name);
}
else
{
System.out.print(" <"+c.getName()+">"); // <------------------ Début Sous-Noeud
System.out.print(c.getText()) ;
System.out.println("</"+c.getName()+">"); // <------------------ Fin Sous-Noeud
}
System.out.println(" </"+c.getName()+">"); // <---------------------- Fin Noeud
//Extraire les donnees (attributes, values ...) ou parcourir en
// profondeur en fonction de child (noeud terminal ou arbre ....)
}
System.out.println("</"+root.getName()+">"); // <---------------------- Fin Racine
}
catch (JDOMException e)
{
System.err.println(e.getMessage());
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
return document;
}
// ====================================== Item ajouté ====================================== \\
private static void addElementIn(Element element, String elementName, String textValue) {
Element newChild = new Element(elementName);
newChild.setText(textValue);
element.addContent(newChild);
}
// ====================================== Item ajouté ====================================== \\
// ====================================== Sauvegarde ====================================== \\
static void enregistreFichier(Document document, String fichier) throws Exception // ajoute le paramètre document
{
XMLOutputter sortie = new XMLOutputter();
sortie.output(document, new FileOutputStream(fichier)); // c'est le paramètre qui est utilisé et non plus la variable static
}
private static void addElementAfter(Element element, String elementName, String textValue) {
Parent parent = element.getParent();
Element newChild = new Element(elementName);
newChild.setText(textValue);
parent.addContent(parent.indexOf(element)+1, newChild);
}
}
public class ComponentHandler {
public final JPanel pane;
public final JLabel label;
public ComponentHandler(JPanel pane, JLabel label) {
this.pane=pane;
this.label=label;
}
} |
Partager