2010-04-02 4 views
6

Je l'ai vu le poste qui traitent de cette question, mais je ne peux toujours pas résoudre mon problème:jQuery et XML (avec CDATA)

J'ai XML avec CDATA et quand je parser le XML, il inclut le CDATA (que je ne veux pas).

exemple XML:

<mainnav> 
    <nav path="/" xmlpath="home.xml" key="footer" navigator=""> 
     <display><![CDATA[Home]]></display> 
     <title><![CDATA[Home]]></title> 
    </nav> 

    <nav path="/nav1/" xmlpath="nav1.xml" key="primary" navigator="primary" iconid="0"> 
     <display><![CDATA[Nav 1]]></display> 
     <title><![CDATA[Nav 1]]></title> 
     <overdesc><![CDATA[test nav 1]]></overdesc> 

     <sub path="/nav1/sub1/" xmlpath="nav1/sub1.xml" key="sub"> 
      <display><![CDATA[sub 1<br />of nav 1]]></display> 
      <title><![CDATA[sub 1<br />of nav 1]]></title> 
     </sub> 

    </nav> 


    <nav path="/nav1/" xmlpath="nav2.xml" key="primary" navigator="primary" iconid="1"> 
     <display><![CDATA[Nav 2]]></display> 
     <title><![CDATA[Nav 2]]></title> 
     <overdesc><![CDATA[test nav 2]]></overdesc> 

     <sub path="/nav2/sub1/" xmlpath="nabv2/sub1.xml" key="sub"> 
      <display><![CDATA[sub 1<br />of nav 2]]></display> 
      <title><![CDATA[sub 1<br />of nav2]]></title> 
     </sub> 

    </nav> 

</mainnav> 

jQuery:

$(document).ready(function(){ 
$.ajax({ 
    type: "GET", 
    url: "site_xml/config.xml", 
    //contentType: "text/xml", 
    dataType: ($.browser.msie) ? "xml" : "text/xml", 
    success: parseXML, 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
     alert(errorThrown); 
    } 
});}); 

function parseXML(xml) { 
$(xml).find('nav').each(function(){ 
    if ($(this).attr("key")=="primary") { // this is a primary nav item; 
     var title = $.trim($(this).find('title').text()); 
     alert(title); 
     $("#output").append(title); //nothing showing up in my output DIV, presumably due to the CDATA tags? 
    } 
}); 

}

Répondre

10

On dirait qu'il ya deux Chil Dren nommé titre dans l'étiquette de navigation. Vous obtenez de retour à la fois quand vous faites:

$(this).find("title").text() 

Essayez d'utiliser:

$(this).find("title:first").text() 

En outre, retirer le conditionnel:

dataType: ($.browser.msie) ? "xml" : "text/xml", 

Et il suffit d'utiliser:

dataType: "xml", 
+0

oh merci. le dataType a fait l'affaire. d'oh! mais IE6 n'affichera rien maintenant. des idées? – Pico

+0

Souhaitez-vous tester ceci localement? IE6 ne semble pas aimer ça. Probablement parce que les en-têtes corrects ne sont pas envoyés comme ils sont quand ils sont sur un serveur web. Essayez de placer le fichier XML sur un serveur Web ou utilisez-le pour tester: http://digitalpadin.com/test.xml Source: http://groups.google.com/group/jquery-fr/browse_thread/ thread/adb2b047f3761179? pli = 1 – Sandro

+0

tout tourne sur un serveur web. – Pico

0

OK trouvé le morceau manquant sur un autre forum:

<script type="text/javascript"> au lieu de: <script type="application/javascript">

1
<PRODUCTS> 
    <COD>1</COD> 
    <NAME><![CDATA[MINISYSTEM SONY VAIO]]></NAME> 
</PRODUCTS> 


     function CDATA(str){    
      var res = str.substr(9,str.length-12) 
      return res 
     } 

     CDATA($(this).find("name").text()); 
+2

Veuillez ajouter plus de détails. –