(OT Harbour) Xml - Búsqueda bestia

Post Reply
hmpaquito
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

(OT Harbour) Xml - Búsqueda bestia

Post by hmpaquito »

Hola a todos,

Teniendo un xml tal que así:

Code: Select all | Expand

<Principal>
   <Desglose>
      <Auxiliar>
          <Precio>10.1</Precio>
      </Auxiliar>
   </Desglose>
</Principal>
 


Habria alguna forma de hacer una busqueda DIRECTA a "Precio", es decir, buscando el tag "Precio", devuelva la cadena de jerarquia donde se encuentra, que seria en el ejemplo: Principal, Desglose, Auxiliar.
Se me ocurren varias formas, nada ortodoxas, a cual de ellas peor :) pero no quiero reinventar la rueda y tampoco inventar el hilo negro y mucho menos matar moscas a cañonazos. :D
jbrita
Posts: 507
Joined: Mon Jan 16, 2006 3:42 pm

Re: (OT Harbour) Xml - Búsqueda bestia

Post by jbrita »

prueba asi

local cSrcFileName := "Archivo.xml"
local cNodeName := "Precio"
local cNodeString := ''
local cNodeBegTag := "<Precio"
local cNodeEndTag := "</Precio>"
local cSrcString

cSrcString := HB_MEMOREAD( cSrcFileName )
cNodeString:= HL_ExtrcSIS( cSrcString, cNodeBegTag, cNodeEndTag )
? "<Precio"+cNodeString+"</Precio>"

RETURN
procedure HL_ExtrcSIS(; // Extract String IN String
cUpStr,; // Up-String
cDelm1,; // Delimiter - 1
cDelm2) // Delimiter - 2

LOCAL cRVal := '',;
nPos1 := 0,;
nPos2 := 0

IF !EMPTY( cUpStr ) .AND. !EMPTY( cDelm1 ) .AND. (!EMPTY( cDelm2 ) .OR. cDelm2 == CRLF ) // A517..21

IF cDelm1 $ cUpStr
nPos1 := AT( cDelm1, cUpStr ) + LEN( cDelm1 )
IF cDelm2 $ SUBS( cUpStr, nPos1 )
inkey(.8)
nPos2 := HL_PAT( nPos1, cDelm2, cUpStr )
cRVal := SUBS( cUpStr, nPos1, nPos2-nPos1 )
cUpStr := SUBS( cUpStr, 1, nPos1 - LEN( cDelm1 ) - 1 ) + SUBS( cUpStr, nPos2 + LEN(cDelm2) )
ELSE
cRVal := SUBS( cUpStr, nPos1 )
cUpStr := LEFT( cUpStr, nPos1 )
ENDIF cDelm2 $ SUBS( cUpStr, nPos1 )
ENDIF cDelm1 $ cUpStr

ENDIF !EMPTY( cUpStr ) .AND. !EMPTY( cDelm1 ) .AND. !EMPTY( cDelm2 ) // A517

RETU cRVal // HL_ExtrcSIS()


PROCEDURE HL_PAT(nBasPos,cAraKtr, cInKtr)

LOCAL nRVal := 0

IF nBasPos <= LEN( cInKtr )
nRVal := AT( cAraKtr, SUBS( cInKtr, nBasPos ) )
IF nRVal > 0
nRVal := nBasPos + nRVal - 1
ENDI
ENDIF nBasBos <= LEN( cAraKtr )

RETU nRVal

SALUDOS
hmpaquito
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: (OT Harbour) Xml - Búsqueda bestia

Post by hmpaquito »

jbrita,

Gracias por tu codigo

¿ lo tienes en produccion ?
¿ Tendrías algo similar pero para hashes ?

Saludos
User avatar
thefull
Posts: 731
Joined: Fri Oct 07, 2005 7:42 am
Location: Barcelona
Contact:

Re: (OT Harbour) Xml - Búsqueda bestia

Post by thefull »

Mira de usar mxmlFindPath
https://michaelrsweet.github.io/mxml/mxml.html#3_8

Y si no, puedes convertir tu xml a un hash a traves de xmltohash() , http://xthefull.blogspot.com.es/2017/04/convertir-xml-hash-en-harbour-update-v.html
Saludos
Rafa Carmona ( rafa.thefullARROBAgmail.com___quitalineas__)
hmpaquito
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: (OT Harbour) Xml - Búsqueda bestia

Post by hmpaquito »

Rafa, gracias por la respuesta.

Yo habia pensado en un hb_hascan() que buscara una key y devolviera los nodos padre para llegar hasta el.
La verdad, pensé que quizá ya lo tenía harbour, puesto que me pareció una operación de hashes que podría ser habitual.

Saludos.
Post Reply