Função converte Hash para XML
Enviado: 07 Ago 2025 08:42
Olá!
Atenção, precisa do MINIXML na pasta \contrib\hbmxml do harbour adicionar ao seu projeto hbmxml.hbc
Autor Przemyslaw Czerpak
Saudações,
Itamar M. Lins Jr.
Atenção, precisa do MINIXML na pasta \contrib\hbmxml do harbour adicionar ao seu projeto hbmxml.hbc
Código: Selecionar todos
/* ************************************************************************ */
function hb_hashToXML( hValue, n, cEOL )
LOCAL cXML
if valtype( n ) != "N"
n := -2
endif
if cEOL == NIL
cEOL := iif( n >= -1, hb_eol(), "" )
endif
cXML := hb_XML_savenodes( hValue, n, cEOL )
/* add header */
return '<?xml version="1.0" encoding="UTF-8"?>' + hb_eol() + ;
hb_strToUtf8( cXML ) + hb_eol()
static function hb_XML_savenodes( hValue, n, cEOL )
static s_cXmlEscape := "<>&"
static s_aXmlEscape := { "<", ">", "&" }
local cXML := "", v, e, x, n2
n2 := iif( n < 0, n, n + 2 )
for each v in hValue
switch valtype( v )
case "C"
case "M"
x := hb_strReplace( v, s_cXmlEscape, s_aXmlEscape )
exit
case "N"
x := hb_ntos( v )
exit
case "D"
x := dtos( v )
exit
case "T"
x := hb_tstostr( v )
exit
case "L"
x := iif( v, "Y", "N" )
exit
case "H"
cXML += space( n ) + "<" + v:__enumKey() + ">" + cEOL
cXML += hb_XML_savenodes( v, n2, cEOL )
cXML += space( n ) + "</" + hb_XML_endtag( v:__enumKey() ) + ">" + cEOL
x := NIL
exit
case "A"
for each e in v
if valtype( e ) == "H"
cXML += space( n ) + "<" + v:__enumKey() + ">" + cEOL
cXML += hb_XML_savenodes( e, n2, cEOL )
cXML += space( n ) + "</" + hb_XML_endtag( v:__enumKey() ) + ">" + cEOL
else
cXML += hb_XML_savenodes( { v:__enumKey() => e }, n, cEOL )
endif
next
x := NIL
exit
otherwise
x := NIL
endswitch
if x != NIL
cXML += space( n ) + "<" + v:__enumKey() + ">"
cXML += x
cXML += "</" + hb_XML_endtag( v:__enumKey() ) + ">" + cEOL
endif
next
return cXML
static function hb_XML_endtag( cTag )
local n := at( " ", cTag )
if n != 0
cTag := left( cTag, n - 1 )
endif
return cTag
/* ************************************************************************ */
/* use MiniXML to decode XML data */
#include "hbmxml.ch"
function hb_XMLtoHash( cXML )
local pXML
pXML := mxmlLoadString( NIL, hb_utf8ToStr( cXML ), MXML_OPAQUE_CALLBACK )
if !empty( pXML )
/* skip header */
return hb_XML_getnodes( mxmlGetFirstChild( pXML ) )
endif
return NIL
static function hb_XML_getnodes( pNode )
local cKey, pChild, xResult, xValue, aValue
while !empty( pNode )
if mxmlGetType( pNode ) == MXML_ELEMENT
cKey := mxmlGetElement( pNode )
pChild := mxmlGetFirstChild( pNode )
xValue := hb_XML_getnodes( pChild )
if xValue == NIL
xValue := mxmlGetOpaque( pNode )
endif
if xResult == NIL
if cKey = "![CDATA["
xResult := mxmlGetCDATA( pNode )
if right( xResult, 2 ) == "]]"
xResult := hb_strShrink( xResult, 2 )
endif
exit
endif
xResult := {=>}
hb_hKeepOrder( xResult, .t. )
xResult[ cKey ] := xValue
elseif cKey $ xResult
aValue := xResult[ cKey ]
if !hb_isArray( aValue )
aValue := { aValue }
xResult[ cKey ] := aValue
endif
aadd( aValue, xValue )
else
xResult[ cKey ] := xValue
endif
endif
pNode := mxmlGetNextSibling( pNode )
enddo
return xResult
/* ************************************************************************ */
Saudações,
Itamar M. Lins Jr.