insertChild

on a du xml :

  1. var x : XML =
  2. <books>
  3. <book title="Harry potter et l'ordre de phoenix">
  4. <chapitre nom="HPOPchapitre 1" test="B"/>
  5. <chapitre nom="HPOPchapitre 2" test="B"/>
  6. <chapitre nom="HPOPchapitre 3" test="B"/>
  7. </book>
  8. <book title="Harry Potter et prince de sang mele">
  9. <chapitre nom="HPPSMchapitre 1" test="a"/>
  10. <chapitre nom="HPPSMchapitre 2" test="yoy"/>
  11. <chapitre nom="HPPSMchapitre 3" test="B"/>
  12. </book>
  13. <book title="Harry potter et les reliques de la Mort">
  14. <chapitre nom="HPRMchapitre 1" test="B"/>
  15. <chapitre nom="HPRMchapitre 2" test="B"/>
  16. <chapitre nom="HPRMchapitre 3" test="B"/>
  17. </book>
  18. </books> ;

on voudrait ajouter un livre:

  1. var book : XML = <book title="StarWars" /> ;

on lit la doc (http://livedocs.adobe.com/labs/flex3/html/help.html?content=13_Working_with_XML_07.html) et on trouve un truc du genre

  1. x = x.insertChildAfter(x.book[0], book);

Et x donne ca :

  1. <books>
  2. <book title="Harry Potter et l'ordre du phoenix">
  3. <chapitre nom="HPOPchapitre 1" test="B"/>
  4. <chapitre nom="HPOPchapitre 2" test="B"/>
  5. <chapitre nom="HPOPchapitre 3" test="B"/>
  6. </book>
  7. <book title="StarWars"/>
  8. <book title="Harry Potter et prince de sang mele">
  9. <chapitre nom="HPPSMchapitre 1" test="a"/>
  10. <chapitre nom="HPPSMchapitre 2" test="yoy"/>
  11. <chapitre nom="HPPSMchapitre 3" test="B"/>
  12. </book>
  13. <book title="Harry potter et les reliques de la Mort">
  14. <chapitre nom="HPRMchapitre 1" test="B"/>
  15. <chapitre nom="HPRMchapitre 2" test="B"/>
  16. <chapitre nom="HPRMchapitre 3" test="B"/>
  17. </book>
  18. </books>

Cool ca marche :)

Et puis bon on se dit qu'on a bien compris, alors pourquoi pas rajouter un chapitre... on tente :

  1. var chapitre : XML = <chapitre nom="HPRMchapitre2.5" /> ;
  2. x = x.insertChildAfter(x.book[0].chapitre[1], chapitre);

mais ça marche pas... pourtant on fait tout comme la documentation ??? En fait, pour insérer un noeud xml il faut surtout appeler insertChildAfter sur le noeud parent du noeud qui sera inséré...

  1. var after : XML = x.book[1].chapitre[1];
  2. var after_parent = after.parent();
  3. after_parent = after_parent.insertChildAfter(after,chapitre);

et la ca marche... plutôt bien

  1. <books>
  2. <book title="Harry potter et l'ordre de phoenix">
  3. <chapitre nom="HPOPchapitre 1" test="B"/>
  4. <chapitre nom="HPOPchapitre 2" test="B"/>
  5. <chapitre nom="HPOPchapitre 3" test="B"/>
  6. </book>
  7. <book title="Harry Potter et prince de sang mele">
  8. <chapitre nom="HPPSMchapitre 1" test="a"/>
  9. <chapitre nom="HPPSMchapitre 2" test="yoy"/>
  10. <chapitre nom="HP 2.5"/>
  11. <chapitre nom="HPPSMchapitre 3" test="B"/>
  12. </book>
  13. <book title="Harry potter et les reliques de la Mort">
  14. <chapitre nom="HPRMchapitre 1" test="B"/>
  15. <chapitre nom="HPRMchapitre 2" test="B"/>
  16. <chapitre nom="HPRMchapitre 3" test="B"/>
  17. </book>
  18. </books>

Les requetes

Sur des noeuds XML, on peut faire des requètes rapides à écrire, Par exemple : Tout les chapitres , à n'importe quelle profondeur

  1. var chap : XMLList = x..chapitre;

On peut compliquer en voulant le chapitre dont le paramètres test vaut yoy... (<chapitre nom="HPPSMchapitre 2" test="yoy"/>)

  1. var chap : XMLList = x..chapitre.(@test=="yoy");

Sauf que là, ça marche que si tout les noeuds chapitre ont cet attribut test, sinon ca génère un erreur...

Passer du XMLList au XML

Bon ca, c'est plus un pense bête, parce que ce matin, étant pas très réveillé, ca me paraissait super intéressant, beaucoup moins maintenant... Concernant les requètes, elles sont super pratiques, facile à écrire ! On peux s'en servir pour retrouver un noeud précis dans le xml par exemple, pour utiliser ensuite insertChildAfter. Sauf que insertChildAfter, lui il prend du XML et pas du XMLList. La solution, pour récupérer du XML, quand on utilise ce genre de requète, c'est d'utiliser les opérateurs pour accèder à l'objet XML contenu dans la XMLList.

  1. var chap : XML = x..chapitre.(@test=="yoy")[0];

Voilà j'avais prévenu, c'est pas la découverte du siècle :)