Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

[Jdom]déplacer un élémnt à l'"intérieur" du parent

9 réponses
Avatar
Daniel Moyne
je n'arrive pas à permuter deux élements dans un fichier jdom.

Voici ce que je fais et qui ne fonctionne pas :

recordsElement = documentRoot.getChild(MARRIAGE_XML_RECORDS);
myListOfMarriageElements=recordsElement.getChildren();

public int getIndexOfElementInListOfMarriageElements(Element element) {
return myListOfMarriageElements.indexOf(element);
}


public void swapMarriageRecordsInJDOM(Element element1, Element element2) {
int index1 = getIndexOfElementInListOfMarriageElements(element1);
int index2 = getIndexOfElementInListOfMarriageElements(element2);
element1.detach();
element2.detach();
if (index1 < index2) {
recordsElement.addContent(index1, element2);
recordsElement.addContent(index2, element1);
} else {
recordsElement.addContent(index2, element1);
recordsElement.addContent(index1, element2);
}
}

il y a mouvement mais c'est n'importe quoi !

9 réponses

Avatar
Xavier Nayrac
Daniel Moyne a écrit :
je n'arrive pas à permuter deux élements dans un fichier jdom.

Voici ce que je fais et qui ne fonctionne pas :

recordsElement = documentRoot.getChild(MARRIAGE_XML_RECORDS);
myListOfMarriageElements=recordsElement.getChildren();

public int getIndexOfElementInListOfMarriageElements(Element element) {
return myListOfMarriageElements.indexOf(element);
}


public void swapMarriageRecordsInJDOM(Element element1, Element element2) {
int index1 = getIndexOfElementInListOfMarriageElements(element1);
int index2 = getIndexOfElementInListOfMarriageElements(element2);
element1.detach();
element2.detach();
if (index1 < index2) {
recordsElement.addContent(index1, element2);
recordsElement.addContent(index2, element1);
} else {
recordsElement.addContent(index2, element1);
recordsElement.addContent(index1, element2);
}
}

il y a mouvement mais c'est n'importe quoi !




Je ne connais pas du tout l'API JDOM, alors je vais peut-être dire une
ânerie.
La méthode detach() retire l'élément du parent, c'est ça ?
Donc je me demande si les index sont toujours d'actualité après
l'utilisation de detach().

--
Xavier Nayrac
http://personalbugtracker.free.fr
Avatar
Christian Laborde
Est-ce vraiment n'importe quoi ? L'erreur est-elle
reproductible ou aléatoire ? Peut-on avoir une liste des
éléments avant et après l'opération ?

Daniel Moyne a écrit :
je n'arrive pas à permuter deux élements dans un fichier jdom.

Voici ce que je fais et qui ne fonctionne pas :

recordsElement = documentRoot.getChild(MARRIAGE_XML_RECORDS);
myListOfMarriageElements=recordsElement.getChildren();

public int getIndexOfElementInListOfMarriageElements(Element element) {
return myListOfMarriageElements.indexOf(element);
}


public void swapMarriageRecordsInJDOM(Element element1, Element element2) {
int index1 = getIndexOfElementInListOfMarriageElements(element1);
int index2 = getIndexOfElementInListOfMarriageElements(element2);
element1.detach();
element2.detach();
if (index1 < index2) {
recordsElement.addContent(index1, element2);
recordsElement.addContent(index2, element1);
} else {
recordsElement.addContent(index2, element1);
recordsElement.addContent(index1, element2);
}
}

il y a mouvement mais c'est n'importe quoi !




--
Christian Laborde
La Révolution citoyenne, c'est sur : http://c.lab.over-blog.com/
Le forum des électrons libres :
http://electrons-libres.forumactif.fr
Les citoyens qui voient Net : http://www.netoyens.info
True E-mail : remove -no-spam-
Sentier des Vinches
CH 1091 Grandvaux
Suisse
Avatar
JavaBeaucoupMieux
"Daniel Moyne" a écrit dans le message de news:
4ab7d41f$0$18484$
je n'arrive pas à permuter deux élements dans un fichier jdom.

Voici ce que je fais et qui ne fonctionne pas :

recordsElement = documentRoot.getChild(MARRIAGE_XML_RECORDS);
myListOfMarriageElements=recordsElement.getChildren();





public int getIndexOfElementInListOfMarriageElements(Element element)
{
return myListOfMarriageElements.indexOf(element);
}




Quel est l'intérêt de cette méthode ? Il suffit d'utiliser .indexOf



public void swapMarriageRecordsInJDOM(Element element1, Element
element2) {
int index1 = getIndexOfElementInListOfMarriageElements(element1);



int index1=myListOfMarriageElements.indexOf(element1);


int index2 = getIndexOfElementInListOfMarriageElements(element2);
element1.detach();
element2.detach();
if (index1 < index2) {
recordsElement.addContent(index1, element2); element2 =>
index1
recordsElement.addContent(index2, element1); element1 =>
index2
} else {
recordsElement.addContent(index2, element1); element1 =>
index2
recordsElement.addContent(index1, element2); element2 =>
index1
}
}

il y a mouvement mais c'est n'importe quoi !




Le même traitement est appliqué que index1 soit plus petit ou non que
index2...
Avatar
Christian Laborde
Non. Il faut tenir compte du décalage d'index après
insertion. Donc faire les insertions dans un ordre différents.

JavaBeaucoupMieux a écrit :

"Daniel Moyne" a écrit dans le message de news:
4ab7d41f$0$18484$
je n'arrive pas à permuter deux élements dans un fichier jdom.

Voici ce que je fais et qui ne fonctionne pas :

recordsElement = documentRoot.getChild(MARRIAGE_XML_RECORDS);
myListOfMarriageElements=recordsElement.getChildren();





public int getIndexOfElementInListOfMarriageElements(Element element) {
return myListOfMarriageElements.indexOf(element);
}




Quel est l'intérêt de cette méthode ? Il suffit d'utiliser .indexOf



public void swapMarriageRecordsInJDOM(Element element1, Element
element2) {
int index1 = getIndexOfElementInListOfMarriageElements(element1);



int index1=myListOfMarriageElements.indexOf(element1);


int index2 = getIndexOfElementInListOfMarriageElements(element2);
element1.detach();
element2.detach();
if (index1 < index2) {
recordsElement.addContent(index1, element2); element2 => index1
recordsElement.addContent(index2, element1); element1 => index2
} else {
recordsElement.addContent(index2, element1); element1 => index2
recordsElement.addContent(index1, element2); element2 => index1
}
}

il y a mouvement mais c'est n'importe quoi !




Le même traitement est appliqué que index1 soit plus petit ou non que
index2...







--
Christian Laborde
La Révolution citoyenne, c'est sur : http://c.lab.over-blog.com/
Le forum des électrons libres :
http://electrons-libres.forumactif.fr
Les citoyens qui voient Net : http://www.netoyens.info
True E-mail : remove -no-spam-
Sentier des Vinches
CH 1091 Grandvaux
Suisse
Avatar
JavaBeaucoupMieux
"Christian Laborde" a écrit dans le message de news:
h9odq3$grc$
Non. Il faut tenir compte du décalage d'index après
insertion. Donc faire les insertions dans un ordre différents.




Démonstration :
===========
A-B-C-D-E-F-G-H

element1 : C index1 : 2
element2 : G index2 : 6

Detach...

A-B-D-E-F-H

addContent(2,G) ==> A-B-G-D-E-F-H
addContent(6,C) ==> A-B-G-D-E-F-C-H

ou

addContent(6,C) ==> A-B-D-E-F-H-C
addContent(2,G) ==> A-B-G-D-E-F-H-C

----------------------------------------------------------------------------------------------

A-B-C-D-E-F-G-H

element1 : G index1 : 6
element2 : C index2 : 2

Detach...

A-B-D-E-F-H

addContent(6,C) ==> A-B-D-E-F-H-C
addContent(2,G) ==> A-B-G-D-E-F-H-C

ou

addContent(2,G) ==> A-B-G-D-E-F-H
addContent(6,C) ==> A-B-G-D-E-F-C-H

----------------------------------------------------------------------------------------------

Donc, on doit bien insérer d'abord l'élément d'index plus faible...

D'où une petite question : est-ce qu'on est sûr que le déroulement des
opérations est bien séquentiel ? Ne faut-il pas s'assurer de la synchronisation
de l'algorithme ?
Avatar
JavaBeaucoupMieux
"Christian Laborde" a écrit dans le message de news:
h9odq3$grc$
Non. Il faut tenir compte du décalage d'index après
insertion. Donc faire les insertions dans un ordre différents.




Démonstration :
===========
A-B-C-D-E-F-G-H

element1 : C index1 : 2
element2 : G index2 : 6

Detach...

A-B-D-E-F-H

addContent(2,G) ==> A-B-G-D-E-F-H
addContent(6,C) ==> A-B-G-D-E-F-C-H

ou

addContent(6,C) ==> A-B-D-E-F-H-C
addContent(2,G) ==> A-B-G-D-E-F-H-C

----------------------------------------------------------------------------------------------

A-B-C-D-E-F-G-H

element1 : G index1 : 6
element2 : C index2 : 2

Detach...

A-B-D-E-F-H

addContent(6,C) ==> A-B-D-E-F-H-C
addContent(2,G) ==> A-B-G-D-E-F-H-C

ou

addContent(2,G) ==> A-B-G-D-E-F-H
addContent(6,C) ==> A-B-G-D-E-F-C-H

----------------------------------------------------------------------------------------------

Donc, on doit bien insérer d'abord l'élément d'index plus faible...

D'où une petite question : est-ce qu'on est sûr que le déroulement des
opérations est bien séquentiel ? Ne faut-il pas s'assurer de la synchronisation
de l'algorithme ?
Avatar
Daniel Moyne
"JavaBeaucoupMieux" <@> wrote:


"Daniel Moyne" a écrit dans le message de news:
4ab7d41f$0$18484$
je n'arrive pas à permuter deux élements dans un fichier jdom.

Voici ce que je fais et qui ne fonctionne pas :

recordsElement = documentRoot.getChild(MARRIAGE_XML_RECORDS);
myListOfMarriageElements=recordsElement.getChildren();





public int getIndexOfElementInListOfMarriageElements(Element element)
{
return myListOfMarriageElements.indexOf(element);
}




Quel est l'intérêt de cette méthode ? Il suffit d'utiliser .indexOf


trouver l'index dans la liste
public void swapMarriageRecordsInJDOM(Element element1, Element
element2) {
int index1 = getIndexOfElementInListOfMarriageElements(element1);



int index1=myListOfMarriageElements.indexOf(element1);


idem
int index2 = getIndexOfElementInListOfMarriageElements(element2);
element1.detach();
element2.detach();
if (index1 < index2) {
recordsElement.addContent(index1, element2); element2 =>
index1
recordsElement.addContent(index2, element1); element1 =>
index2
} else {
recordsElement.addContent(index2, element1); element1 =>
index2
recordsElement.addContent(index1, element2); element2 =>
index1
}
}

il y a mouvement mais c'est n'importe quoi !




Le même traitement est appliqué que index1 soit plus petit ou non que
index2...


faux
Amicalement.
Avatar
Daniel Moyne
Xavier Nayrac wrote:

Daniel Moyne a écrit :
je n'arrive pas à permuter deux élements dans un fichier jdom.

Voici ce que je fais et qui ne fonctionne pas :

recordsElement = documentRoot.getChild(MARRIAGE_XML_RECORDS);
myListOfMarriageElements=recordsElement.getChildren();

public int getIndexOfElementInListOfMarriageElements(Element element) {
return myListOfMarriageElements.indexOf(element);
}


public void swapMarriageRecordsInJDOM(Element element1, Element element2)
{
int index1 = getIndexOfElementInListOfMarriageElements(element1);
int index2 = getIndexOfElementInListOfMarriageElements(element2);
element1.detach();
element2.detach();
if (index1 < index2) {
recordsElement.addContent(index1, element2);
recordsElement.addContent(index2, element1);
} else {
recordsElement.addContent(index2, element1);
recordsElement.addContent(index1, element2);
}
}

il y a mouvement mais c'est n'importe quoi !




Je ne connais pas du tout l'API JDOM, alors je vais peut-être dire une
ânerie.
La méthode detach() retire l'élément du parent, c'est ça ?
Donc je me demande si les index sont toujours d'actualité après
l'utilisation de detach().


C'est effectivement un point important auquel je n'avais pas pensé et qui
peut expliquer la confusion au moment de la réinsertion des éleéents dans la
liste, mais j'ai complètement abandonné cette méthode au profit d'une
intervention directe sur la liste des éléments ; voir message suivant.
Avatar
Daniel Moyne
Daniel Moyne wrote:

je n'arrive pas à permuter deux élements dans un fichier jdom.

Voici ce que je fais et qui ne fonctionne pas :

recordsElement = documentRoot.getChild(MARRIAGE_XML_RECORDS);
myListOfMarriageElements=recordsElement.getChildren();

public int getIndexOfElementInListOfMarriageElements(Element element) {
return myListOfMarriageElements.indexOf(element);
}


public void swapMarriageRecordsInJDOM(Element element1, Element element2)
{
int index1 = getIndexOfElementInListOfMarriageElements(element1);
int index2 = getIndexOfElementInListOfMarriageElements(element2);
element1.detach();
element2.detach();
if (index1 < index2) {
recordsElement.addContent(index1, element2);
recordsElement.addContent(index2, element1);
} else {
recordsElement.addContent(index2, element1);
recordsElement.addContent(index1, element2);
}
}

il y a mouvement mais c'est n'importe quoi !


Je me réponds à moi-même car très souvent quand on pose une question sur un
forum on continue à travailler sur le problème posé ; tout d'abord je tiens
à m'excuser pour le retard apporter à mes réponses aux divers intervenants
sur la question posée mais j'ai du aller aux obsèques de ma belle-sœur dans
un autre endroit de France et ainsi m'absenter pour presque 2 semaines.

Revenons à nos moutons...

Comme je le disais à un des intervenants je travaille désormais directement
sur les listes d'éléments pour modifier l'ordre des éléments, en suprrimer
ou en ajouter car j'ai trouvé ce paragraphe très intéressant sur un site
parlants d'ajouts d'éléments à un document jdom :

<begin>
List children = element.getChildren();
List children = element.getChildren
( name );
List children = element.getChildren
( name, namespace );

These methods return a list of child elements belonging to the Element,
element. If no children exist, the returned list will be empty.

Any changes to the returned list object will automatically be reflected in
the underlying JDOM document. Since each of these methods return a Java 2
List object, then adding, removing, and reordering children are performed
using native Java 2 List operations....
<end>

Donc pour le problème posé on oublie les "addContent" et autre "detach()" et
on applique le même algorithme sue la liste d'enfants :

public void swapElementWithOffsetElementInListOfMarriageElements(Element
element1, Element element2) {
int index1 = getIndexOfElementInListOfMarriageElements(element1);
int index2 = getIndexOfElementInListOfMarriageElements(element2);
removeElementFromListOfMarriageElements(element1);
removeElementFromListOfMarriageElements(element2);
(index2 >= index1) {
addElementToListOfMarriageElementsAt(index1, element2);
addElementToListOfMarriageElementsAt(index2, element1);
} else {
addElementToListOfMarriageElementsAt(index2, element1);
addElementToListOfMarriageElementsAt(index1, element2);
}
}

où :
public void addElementToListOfMarriageElementsAt(int index, Element element)
{
myListOfMarriageElements.add(index, element);
}
et :
public void removeElementFromListOfMarriageElements(Element element) {
myListOfMarriageElements.remove(element);
}
avec bien sûr :
List
myListOfMarriageElements=myListOfMarriageElements=recordsElement.getChildren();

La vraie question est maintenant pourquoi toute intervention sur la liste
"myListOfMarriageElements" est automatiquement "réléchi" (désolé la
traduction de l'anglais manque de précision) dans le document jdom source.

Je pense, au risque de me tromper, que dans jdom une interface DataListener
a été réécrite pour surcharger les méthodes add, remove etc.. des listes
d'éléments du jdom ; c'est d'ailleurs une coïncidence, mais je fais la même
chose dans mon application pour mettre à jour les listes associées à une
collection de jcombobox par simple ajout ou retrait dans un seul vecteur
d'objets ; de manière plus explicite, si par exemple je fais :
vector.add(element)
toutes les jcombobox affichent un liste d'objets mise à jour et comme il
s'agit de texte, en plus réordonnée par ordre alphabétique.... Donc là aussi
le "add" fait bien plus qu'un simple ajout dans le vecteur mentionné ici (et
j'en sais quelque chose puisque je sais exactement ce que cette méthode fait
!). Dans mon cas pour bien montrer que ce "add" est particulier je l'ai
rebaptisé en "addElement" pour distinguer les 2 méthodes.

Pour ajouter de l'eau à mon moulin si on fait :

myListOfMarriageElements=new
ArrayList<Element>(recordsElement.getChildren());

ou :

myListOfMarriageElements=new
CopyOnWriteArrayList<Element>(recordsElement.getChildren());

plus rien de ce qui est dit ci-dessus ne marche.

Amicalement.