En parcourant XML avec jQuery

J'ai une base de code qui peut faire une boucle par du XML généré à partir d'Adobe RoboHelp (pour notre documentation d'aide). Cela fonctionne bien, mais comme un sujet pourrait être imbriquées autant de fois que l'auteur veut, j'ai besoin d'un meilleur moyen pour faire une boucle par le biais de ce XML plutôt que de simplement l'imbrication .each() boucles.

Voici ce que le XML ressemble à

<?xml version="1.0" encoding="utf-8"?>
<!--RoboML: Table of Content-->
<roboml_toc>
<page title="Welcome" url="Welcome.htm"/>
<book title="Getting Started" url="Getting_Started/Initial_Setup.htm">
<page title="Initial Setup" url="Getting_Started/Initial_Setup.htm"/>
<page title="Customize Settings" url="Getting_Started/Settings.htm"/>
</book>
<book title="Administrator Services" url="Administrator_Services/General_Administrator.htm">
<book title="Portal Workspace" url="Administrator_Services/Portal_Workspace/AdminHome.htm">
<page title="Home" url="Administrator_Services/Portal_Workspace/AdminHome.htm"/>
<page title="Portal Accounts" url="Administrator_Services/Portal_Workspace/Portal_Accounts.htm"/>
</book>
<book title="SpamLab" url="Administrator_Services/SpamLab/SpamLab_Admin_General.htm">
<page title="Alerts" url="Administrator_Services/SpamLab/Alerts.htm"/>
<page title="Spam Quarantine" url="Administrator_Services/SpamLab/Admin_Spam_Quarantine_.htm"/>
</book>
</book>
<book title="User Services" url="User_Services/General_User.htm">
<book title="Portal Workspace" url="User_Services/Portal_Workspace/Home.htm">
<page title="Home" url="User_Services/Portal_Workspace/Home.htm"/>
<page title="Self Help" url="User_Services/Portal_Workspace/Self_Help.htm"/>
</book>
<book title="SpamLab" url="User_Services/SpamLab/SpamLab_General.htm">
<page title="Spam Quarantine" url="User_Services/SpamLab/Spam_Quarantine.htm"/>
<page title="Virus Quarantine" url="User_Services/SpamLab/Virus_Quarantine.htm"/>
</book>
<book title="Encryption" url="User_Services/Encryption/Encryption_General.htm">
<page title="Outlook Plug-in" url="User_Services/Encryption/Encryption_Outlook_Plug_in.htm"/>
</book>
</book>
</roboml_toc>

Un <page> est un article, et un <book> est un dossier.

Son de mon code jQuery, qui ne peut regarder à un niveau profond de balises

   //Get the TOC
$tocOutput="";
$.get(tocURL,function(toc){
$(toc).children().each(function(){
$tocOutput+="<li><a href='"+$(this).attr("url")+"'>"+$(this).attr("title")+"</a>";
if(this.tagName=="BOOK"){
$tocOutput+="<ul>";
$(this).find("page").each(function(){
$tocOutput+="<li><a href='"+$(this).attr("url")+"'>"+$(this).attr("title")+"</a></li>";
});
$tocOutput+="</ul>";
}
$tocOutput+="</li>";
});
$("#list").html($tocOutput);

Je sais qu'il y a un meilleur moyen de parcourir tous les éléments et ensuite déterminer si l'élément a des enfants, etc. mais je ne peux pas penser comment le faire.

Toute aide est grandement appréciée!

  • Juste curieux - pourquoi cela doit être fait sur le client? Pourquoi ne pas appliquer une transformation XSLT sur le serveur et d'envoyer le html? Est le xml dynamique? Pourrait le transformer en html pas être mis en cache sur le serveur?
  • Peu importe, heureux que vous ayez trouvé une solution - MAINTENANT ACCEPTER DE KEITH RÉPONSE 🙂
  • JE REFUSE! C'était juste une preuve de concept pour l'instant.... en fait, je voulais juste impressionner certaines personnes. Ils sont doucher moi avec éloges maintenant, alors ça vaut la peine. Finalement, je pense que nous allons le faire côté serveur si.
InformationsquelleAutor Chris Barr | 2009-10-15