2010-04-02 41 views
6

que he visto el post que hacer frente a este problema, pero todavía no puedo resolver mi problema:jQuery y XML (con CDATA)

Tengo XML con CDATA y cuando analizar el XML, se incluye el CDATA (que no quiero).

XML de ejemplo:

<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? 
    } 
}); 

}

Respuesta

10

Parece que hay dos chil dren nombrado título dentro de la etiqueta de navegación. Que está recibiendo de vuelta tanto cuando lo hace:

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

Trate de usar:

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

Además, quitar el condicional:

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

Y sólo tiene que utilizar:

dataType: "xml", 
+0

oh gracias. el tipo de datos hizo el truco. d'oh! pero IE6 no mostrará nada ahora. alguna idea? – Pico

+0

¿Está probando esto localmente? A IE6 no parece gustarle eso. Probablemente porque los encabezados correctos no se envían tal como están cuando están en un servidor web. Trate de poner el archivo XML en un servidor web o usar esto para la prueba: http://digitalpadin.com/test.xml Fuente: http://groups.google.com/group/jquery-en/browse_thread/ thread/adb2b047f3761179? pli = 1 – Sandro

+0

todo se ejecuta en un servidor web. – Pico

0

OK encontró la pieza faltante en otro foro:

<script type="text/javascript"> en lugar 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

Por favor, agrega más información. –