Recorrer un XML usando mxml

Recorrer un XML usando mxml

Postby thefull » Fri Jan 30, 2015 5:15 pm

Buenas os transcribo aqui directamente el articulo ;
http://xthefull.blogspot.com.es/2015/01 ... -mxml.html

Debido a los problemas de memoria usando la clase TXMLDocument, he decidido usar directamente la librería mxml.
Pongo un ejemplo de COMO se obtienen los datos a partir de una cadena que contiene el XML.
Parece una tonteria, pero no he encontrado un ejemplo tan simple como esto.
Espero os ayude

Code: Select all  Expand view

function read_xml()
  Local htree, hNode, cType, hTarifa, hNext, hDias, hDia, hHab, hPrecios
  Local xml := [<?xml version="1.0" encoding="UTF-8"?> ]+;
                [<Precios>]+;
                [    <Tarifa Codigo="BAR" Tipo="DIARIO" ImpuestosIncluidos="true" >]+;
                [        <Habitacion Tipo="DOB1" PAX="1">]+;
                [            <DIAS>]+;
                [                <Dia Precio="1" Fecha="01-08-2013" Estado="A"/>]+;
                [                <Dia Precio="2" Fecha="02-08-2013" Estado="C"/>]+;
                [            </DIAS>]+;
                [        </Habitacion>]+;
                [        <Habitacion Tipo="DOB2" PAX="2">]+;
                [            <DIAS>]+;
                [                <Dia Precio="3.1415" Fecha="01-08-2013" Estado="A"/>]+;
                [                <Dia Precio="3.1415" Fecha="02-08-2013" Estado="A"/>]+;
                [            </DIAS>]+;
                [        </Habitacion>]+;
                [    </Tarifa>]+;
                [</Precios>]

      hTree = mxmlLoadString( nil, xml )

      IF Empty( hTarifa := mxmlFindElement( hTree, hTree, "Tarifa",,, MXML_DESCEND ) )
         OutErr( "Unable to find first <tarifas> element in XML tree!" + hb_eol() )

         mxmlDelete( hTree )
         ErrorLevel( 1 )
         QUIT
      endif  

      Alert( "Tarifa coger atributos:"+ mxmlGetElement( hTarifa )   )
      Alert( "Codigo Tarifa.:" + mxmlElementGetAttr( hTarifa, "Codigo" ) + hb_osnewline() + ;
             "Tipo de Tarifa:" + mxmlElementGetAttr( hTarifa, "Tipo" )   + hb_osnewline() +;
             "Impuestos.....:" + mxmlElementGetAttr( hTarifa, "ImpuestosIncluidos" ) )
   
       hNext := mxmlWalkNext( hTarifa, hTree, MXML_DESCEND )   // Cogo habitacion1 porque hTarifa es el nodo principal
       while hNext != NIL
             Alert( "Hab:" + mxmlElementGetAttr( hNext, "Tipo" ) )
             hDias := mxmlWalkNext( hNext, hTree, MXML_DESCEND )        // Cojo nodo DIAS, descendiendo un nivel
                   hDia  := mxmlWalkNext( hDias, hTree, MXML_DESCEND )  // Cojo nodo DIA , descendiendo un nivel
                   while hDia != NIL
                         alert( "DIA :" + mxmlGetElement( hDia ) + "-->" + mxmlElementGetAttr( hDia, "Precio" )  )
                         alert("      " + mxmlGetElement( hDia ) + "-->" + mxmlElementGetAttr( hDia, "Fecha" )  )
                         alert("      " + mxmlGetElement( hDia ) + "-->" + mxmlElementGetAttr( hDia, "Estado" )  )
                         hDia := mxmlGetNextSibling( hDia ) // Siguiente Node dia, al mismo NIVEL
                   end while    

             hNext := mxmlGetNextSibling( hNext ) // El siguiente nodo es la habitacion, mismo NIVEL
       end while

    alert( "mira:" + MXMLSAVEALLOCSTRING( hTree ))
    mxmlDelete( hTree )
return nil
 
Saludos
Rafa Carmona ( rafa.thefullARROBAgmail.com___quitalineas__)
User avatar
thefull
 
Posts: 729
Joined: Fri Oct 07, 2005 7:42 am
Location: Barcelona

Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 60 guests