Ver Mensaje Individual
  #2 (permalink)  
Antiguo 10/03/2010, 19:08
Mexman
 
Fecha de Ingreso: octubre-2009
Mensajes: 11
Antigüedad: 14 años, 6 meses
Puntos: 0
Respuesta: Bucle ASP con XMLDOM

Bueno al final no le queda a uno más que resolver sus dudas a base de aprender. He tardado lo mio hasta aclararme como trabaja el bucle con XMLDOM, creo que es bastante sencillo el código que voy a poner, así que espero que ayude a mucha gente y bueno si ha alguno le apetece mejorarlo o aconsejarnos pues mejor para todos. Yo os dejo aquí con "FOR" el resultado que conseguí, si alguno se anima y me dice como se consigue (si es posible) con DO WHILE pos me ayudaría a aprender algo más y a quitarme la espinita que no supe como conseguirlo con ese bucle. :)

Bueno pues si a alguno le vale aquí esta:
FICHERO ASP 3.0/Classic:
Código ASP:
Ver original
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
  2. <!-- ////////////////////////////////////////////////// START ///////////////////////////////////////////////////////// -->
  3. <div id="seccion">
  4.     <%
  5.         ' Variables.
  6.             Dim xmlObj, v_item
  7.             Dim v_id, v_link, v_titulo, v_seccion, v_describe
  8.             Dim Salto
  9.            
  10.         'Asigno Variables.
  11.             Salto = Replace (cadena, vbCrLf, "<BR>")
  12.            
  13.         'Creo el objeto de XMLDOM
  14.             Set xmlObj = Server.CreateObject("Microsoft.XMLDOM")
  15.         'No dejo que el archivo se edite si está en uso
  16.             xmlObj.async = False
  17.             xmlObj.setProperty "ServerHTTPRequest", true
  18.             xmlObj.validateOnParse = False
  19.             xmlObj.resolveExternals = False
  20.            
  21.         'Cargo el archivo
  22.             xmlObj.load (Server.MapPath("vid.xml"))        
  23.            
  24.         'Si el archivo XML tiene errores
  25.             If xmlObj.parseError.errorCode <> 0 Then
  26.                 Response.Write("Error de Lectura (Por favor, ponerse en contacto con el webmaster).<br />FilePos: " & xmlObj.parseError.filepos & "<br /> Línea: " & xmlObj.parseError.Line & "<br /> Causa: " & xmlObj.parseError.reason & "<br /> Ocurrió en: " & xmlObj.parseError.srcText & "<br /> Archivo: " & xmlObj.parseError.URL)
  27.                 Response.End               
  28.         'Si no contiene errores continuo.
  29.             Else                   
  30.             'Creo v_item y ejecuto la orden.
  31.                 Set v_item = xmlObj.getElementsByTagName("v_galeria/v_item")
  32.                                                
  33. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''     BUCLE     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  34.             'Recorre todas las filas del resultado
  35.                 For n=0 To v_item.length-1
  36.                      v_id = xmlObj.selectSingleNode("/v_galeria").childNodes(n).childNodes(0).Text '"v_id"
  37.                    v_link = xmlObj.selectSingleNode("/v_galeria").childNodes(n).childNodes(1).Text '"v_link"
  38.                 v_seccion = xmlObj.selectSingleNode("/v_galeria").childNodes(n).childNodes(2).Text '"v_seccion"
  39.                  v_titulo = xmlObj.selectSingleNode("/v_galeria").childNodes(n).childNodes(3).Text '"v_titulo"             
  40.                v_describe = xmlObj.selectSingleNode("/v_galeria").childNodes(n).childNodes(4).Text '"v_describe"
  41. %> 
  42.                 <div id="">
  43.                     <ul>
  44.                         <li><%= v_id %>º</li>
  45.                         <li><%= v_link %></li>
  46.                         <li><%= v_seccion %></li>
  47.                         <li><%= v_titulo %></li>
  48.                         <li><%= v_describe %></li>                                     
  49.                     </ul>
  50.                 </div>
  51.  
  52.     <%  
  53. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''     BUCLE     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''            End If
  54.                 'Pasa a la siguiente fila
  55.                     Next
  56.                 'Libera memoria            
  57.                     'v_item.Close
  58.                     Set v_item = Nothing
  59.                     'xmlObj.Close
  60.                     Set xmlObj = Nothing
  61.                    
  62.                 'Cerramos variables
  63.                     Set v_id = Nothing
  64.                     Set v_link = Nothing
  65.                     Set v_titulo = Nothing                                                  
  66.                     Set v_seccion = Nothing
  67.                     Set v_describe = Nothing
  68.             End If
  69.     %>
  70. </div>
  71. <!-- ////////////////////////////////////////////////// FIN ///////////////////////////////////////////////////////// -->

FICHERO XML:
Código XML:
Ver original
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <v_galeria>
  3.     <v_item>
  4.         <v_id>1</v_id>
  5.         <v_link>http://www.youtube.com/1</v_link>
  6.         <v_titulo>Titulo 1</v_titulo>
  7.         <v_seccion>Seccion 1</v_seccion>
  8.         <v_describe>Describe 1.</v_describe>
  9.     </v_item>
  10.     <v_item>
  11.         <v_id>2</v_id>
  12.         <v_link>http://www.youtube.com/2</v_link>
  13.         <v_titulo>Titulo 2</v_titulo>
  14.         <v_seccion>Seccion 2</v_seccion>
  15.         <v_describe>Describe 2.</v_describe>
  16.     </v_item>
  17.     <v_item>
  18.         <v_id>3</v_id>
  19.         <v_link>http://www.youtube.com/3</v_link>
  20.         <v_titulo>Titulo 3</v_titulo>
  21.         <v_seccion>Seccion 3</v_seccion>
  22.         <v_describe>Describe 3.</v_describe>
  23.     </v_item>
  24.     <v_item>
  25.         <v_id>4</v_id>
  26.         <v_link>http://www.youtube.com/4</v_link>
  27.         <v_titulo>Titulo 4</v_titulo>
  28.         <v_seccion>Seccion 4</v_seccion>
  29.         <v_describe>Describe 4.</v_describe>
  30.     </v_item>
  31.     <v_item>
  32.         <v_id>5</v_id>
  33.         <v_link>http://www.youtube.com/5</v_link>
  34.         <v_titulo>Titulo 5</v_titulo>
  35.         <v_seccion>Seccion 5</v_seccion>
  36.         <v_describe>Describe 5.</v_describe>
  37.     </v_item>
  38. </v_galeria>