Foros del Web » Programando para Internet » ASP Clásico »

[SOLUCIONADO] Problemas al procesar XML

Estas en el tema de Problemas al procesar XML en el foro de ASP Clásico en Foros del Web. Hola a tod@s tengo un problema a la hora de parsear un XML, me explico: le el archivo y cuento cuantos items hay, hasta ahí ...
  #1 (permalink)  
Antiguo 08/09/2014, 04:59
Avatar de angel_dope  
Fecha de Ingreso: noviembre-2002
Ubicación: Valencia
Mensajes: 737
Antigüedad: 21 años, 4 meses
Puntos: 8
Problemas al procesar XML

Hola a tod@s tengo un problema a la hora de parsear un XML, me explico: le el archivo y cuento cuantos items hay, hasta ahí ningún problema. Después muestro desde el item 9 hasta el 13 los valores que quiero y aquí viene le problema. Mi código es el siguiente:

Código ASP:
Ver original
  1. set objXml = server.CreateObject("MSXML2.DOMDocument")
  2.     objxml.async = false
  3.    
  4.     ' Aqui cargamos el archivo en el objeto
  5.     objxml.load(server.mappath("./tmp/xml_wm.xml"))
  6.    
  7.     ' Gestion de errores
  8.     if objxml.parseError.errorCode <> 0 Then
  9.             response.Write("Error de Lectura (Por favor, ponerse en contacto con el webmaster).<br />FilePos: " & objxml.parseError.filepos & "<br /> Línea: " & objxml.parseError.Line & "<br /> Causa: " & objxml.parseError.reason & "<br /> Ocurrió en: " & objxml.parseError.srcText & "<br /> Archivo: " & objxml.parseError.URL)
  10.             response.End()
  11.     end if
  12.     numNoticias = objxml.selectSingleNode("/rss/channel").childNodes.length
  13.     if numNoticias <= 5 then
  14.         numnoticias = 0
  15.     else
  16.         for n = 9 to 13
  17.             response.write "titulo = " & objxml.selectSingleNode("/rss/channel").childNodes(n).childNodes(0).Text
  18.             response.write "<br>enlace = " & objxml.selectSingleNode("/rss/channel").childNodes(n).childNodes(1).Text
  19.             response.write "<br>resumen = " & objxml.selectSingleNode("/rss/channel").childNodes(n).childNodes(12).Text
  20.             response.write "<br><br>"
  21.         next
  22.  
  23.     end if
Con lo que muestra los elementos en función de su posición, pero lo malo es que la posición del último elemento puede variar, lo mismo está el 11, que el 13 o el 19, ya que por medio varía el número de algunas informaciones.

He intentado cambiarlo de forma que fuese

Código ASP:
Ver original
  1. response.write "titulo = " & objxml.selectSingleNode("/rss/channel/item/title")Text

Pero entonces obviamente siempre me saca el mismo valor ya que no se tiene en cuenta el contador. En cambio si pongo esto:

Código ASP:
Ver original
  1. response.write "titulo = " & objxml.selectSingleNode("/rss/channel").childNodes(n).("title").Text

me da un error. Si dejo las comillas dice que se esperaba un identificador y si las quito me dice que el objeto no acepta esa propiedad.

¿Cómo puedo acceder a los valores title, link y description sin depende de la posición que ocupen dentro de ITEM, pero accediendo a ITEM en función del contador?

Salu2 y muchas gracias por adelantado
__________________
Vayamos por Partes :: Jack el Destripador
  #2 (permalink)  
Antiguo 09/09/2014, 02:14
Avatar de angel_dope  
Fecha de Ingreso: noviembre-2002
Ubicación: Valencia
Mensajes: 737
Antigüedad: 21 años, 4 meses
Puntos: 8
Respuesta: Problemas al procesar XML

Bueno, esto ya lo tengo resuelto. Cómo se cuales son los títulos de las secciones que busco, simplemente he hecho que por cada ITEM se recorran todas las secciones, y si coinciden con lo que busco, las muestra. Dejo el código por si a alguien le viene bien.

Código ASP:
Ver original
  1. set objXml = server.CreateObject("MSXML2.DOMDocument")
  2.     objxml.async = false
  3.    
  4.     ' Aqui cargamos el archivo en el objeto
  5.     objxml.load(server.mappath("./tmp/xml_wm.xml"))
  6.    
  7.     ' Gestion de errores
  8.     if objxml.parseError.errorCode <> 0 Then
  9.             response.Write("Error de Lectura (Por favor, ponerse en contacto con el webmaster).<br />FilePos: " & objxml.parseError.filepos & "<br /> Línea: " & objxml.parseError.Line & "<br /> Causa: " & objxml.parseError.reason & "<br /> Ocurrió en: " & objxml.parseError.srcText & "<br /> Archivo: " & objxml.parseError.URL)
  10.             response.End()
  11.     end if
  12.     numNoticias = objxml.selectSingleNode("/rss/channel").childNodes.length
  13.     if numNoticias <= 5 then
  14.         numnoticias = 0
  15.     else
  16.         for n = 9 to 13
  17.             i=0
  18.             j=0
  19.             while j=0
  20.                 if objxml.selectSingleNode("/rss/channel").childNodes(n).childNodes(i).tagname = "title" then
  21.                     response.write "titulo = " & objxml.selectSingleNode("/rss/channel").childNodes(n).childNodes(i).text
  22.                     j=1
  23.                 end if
  24.                 i=i+1
  25.             wend
  26.             i=0
  27.             j=0
  28.             while not j<>0
  29.                 if objxml.selectSingleNode("/rss/channel").childNodes(n).childNodes(i).tagname = "link" then
  30.                     response.write "<br>enlace = " & objxml.selectSingleNode("/rss/channel").childNodes(n).childNodes(i).text
  31.                     j=1
  32.                 end if
  33.                 i=i+1
  34.             wend
  35.             i=0
  36.             j=0
  37.             while not j<>0
  38.                 if objxml.selectSingleNode("/rss/channel").childNodes(n).childNodes(i).tagname = "description" then
  39.                     response.write "<br>Resumen = " & objxml.selectSingleNode("/rss/channel").childNodes(n).childNodes(i).text
  40.                     j=1
  41.                 end if
  42.                 i=i+1
  43.             wend
  44.             i=0
  45.             j=0
  46.             while not j<>0
  47.                 if objxml.selectSingleNode("/rss/channel").childNodes(n).childNodes(i).tagname = "content:encoded" then
  48.                     response.write "<br><br>Total = " & objxml.selectSingleNode("/rss/channel").childNodes(n).childNodes(i).text
  49.                     j=1
  50.                 end if
  51.                 i=i+1
  52.             wend
  53.            
  54.             response.write "<br><br>"
  55.         next
  56.     end if
__________________
Vayamos por Partes :: Jack el Destripador

Etiquetas: asp, parsear, xml
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:04.