/*
* XML.prg
* Clase para crear y manipular archivos XML usando el Activex ChilKat ZIP
* Manual en https://www.chilkatsoft.com/refdoc/xChilkatXmlRef.html
* Ejemplos en https://www.example-code.com/vbscript/xml.asp
* Copyright 2016 Bingen
*
*/
#include "Xailer.ch"
DYNAMIC CreateObject
DYNAMIC XA_Break
//------------------------------------------------------------------------------
CLASS TXml FROM TComponent
DATA oChilkat
DATA oXml
DATA oChild
PUBLISHED:
PROPERTY cDLLName INIT "ChilkatAx.dll"
PROPERTY cActivexName INIT "Chilkat_9_5_0"
PROPERTY cKey INIT "xxxxxxxxxxx" Aquí tu clave de Chilkat
PROPERTY cUtility INIT "Manejo de archivos XML"
PUBLIC:
DATA lInstalled INIT .F. READONLY
DATA nErrorCode INIT 0 READONLY
DATA cErrorDescription INIT "" READONLY
METHOD New( oParent ) CONSTRUCTOR // --> Self
METHOD Create( oParent ) CONSTRUCTOR // --> Self
METHOD Close() // --> Nil
METHOD Free() // --> Nil
METHOD GetXml() Inline ::oXml:GetXml() //Devuelve el contenido del XML
METHOD Load(cFileName) Inline ::oXml:LoadXml(cFileName) //Carga un Xml desde un archivo en disco
METHOD Save(cFileName) Inline ::oXml:SaveXml(cFileName) //Graba un Xml desde a archivo en disco
METHOD Tag(Value) Inline ::oXml:Tag:=Value //Crea el TAG principal del XML
METHOD AddAttribute(cAttrName,xValue) Inline IF(Valtype(xValue)="N",::oXml:AddAttributeInt(cAttrName,xValue),::oXml:AddAttribute(cAttrName,ToString(xValue))) //Añade un atributo al TAG principal del XML
METHOD UpdateAttribute(cAttrName,xValue) Inline IF(Valtype(xValue)="N",::oXml:AddOrUpdateAttributeInt(cAttrName,xValue),::oXml:AddOrUpdateAttribute(cAttrName,ToString(xValue))) //Modifica un atributo al TAG principal del XML
METHOD RemoveAttribute(cAttrName) INLINE ::oXml:RemoveAttribute(cAttrName) //Elimina un atributo del TAG principal del XML
METHOD RemoveAllAttributes() INLINE ::oXml:RemoveAllAttributes() //Elimina todos los atributos del TAG principal del XML
METHOD NewChild(cPathName,xValue) Inline IF(Valtype(xValue)="N",::oXml:NewChildInt2(cPathName,xValue),::oXml:NewChild2(cPathName,ToString(xValue))) //Crea un Child dentro del TAG OJO si está vacio no crea inicio y final de Child en 2 lineas sino que lo mete en una sola
METHOD RemoveChild(cPathName) Inline ::oXml:RemoveChild(cPathName)
METHOD AddAttributeAt(cTagPath, cAttrName , cAttrValue) Inline ::oXml:UpdateAttrAt(cTagPath, .T. , cAttrName , cAttrValue) //Añade un atributo al Path del Child indicado dentro del XML
METHOD UpdateAttributeAt(cTagPath, cAttrName , cAttrValue) Inline ::oXml:UpdateAttrAt(cTagPath, .F. , cAttrName , cAttrValue) //Modifica un atributo dell Path del Child indicado dentro del XML
METHOD GetContent(aPath) //Devuelve el contenido de un tag indicando todo su Path GetContent({"HEADER","RECEIVER","RCV_NAME}))
METHOD GetNumChildren() Inline ::oXml:NumChildren //Indica cuantos Child tiene
PRIVATE:
ENDCLASS
//------------------------------------------------------------------------------
METHOD New( oParent ) CLASS TXml
//Ver si está instalada la DLL de ChilKat
TRY
::oChilkat := TOleAuto():New( ::cActivexName+".Global" )
IF ValType( ::oChilkat:Version ) == "C"
::lInstalled := .T.
ENDIF
CATCH
::lInstalled := .F.
END
//Si no esta instalada y no hay DLL
If !::lInstalled .And. !File(::cDllName)
MsgError("No se ha localizado el archivo necesario para "+::cUtility+" "+::cDLLName+CRLF+CRLF+"Contacte con el distribuidor de la aplicación para poder utilizar "+::cUtility,"Error envío "+::cUtility+" 1")
Return .F.
Endif
//Si no esta instalada y si hay DLL intentar registrarla
If !::lInstalled .And. File(::cDLLName)
If !DLLRegisterServer( ::cDLLName )
MsgError("No se ha podido registrar el archivo necesario para "+::cUtility+" "+::cDLLName+CRLF+CRLF+"Debería de ejecutar la aplicación con permisos de administrador una sola vez para poder activar "+::cUtility,"Error envío "+::cUtility+" 2")
Return .F.
Else
//Ver de nuevo si está instalada la DLL de ChilKat
TRY
::oChilkat := TOleAuto():New( ::cActivexName+".Global" )
IF ValType( ::oChilkat:Version ) == "C"
::lInstalled := .T.
ENDIF
CATCH
::lInstalled := .F.
END
Endif
Endif
If !::lInstalled
MsgError("No se ha podido poner en marcha el archivo necesario para "+::cUtility+" "+::cDLLName+CRLF+CRLF+"Contacte con el distribuidor de la aplicación para poder utilizar "+::cUtility,"Error envío "+::cUtility+" 3")
Return .F.
Endif
//Si está instalada meterle la clave de ChilKat
IF ::oChilkat:UnlockBundle(::cKey) <> 1
MsgError("No se ha podido poner en marcha el archivo necesario para "+::cUtility+" "+::cDLLName+CRLF+CRLF+"Contacte con el distribuidor de la aplicación para poder utilizar "+::cUtility,"Error envío "+::cUtility+" 4")
Return .F.
ENDIF
::oChilkat := Nil
::Create( oParent )
RETURN Self
//------------------------------------------------------------------------------
METHOD Create( oParent ) CLASS TXml
::Super:Create( oParent )
::oXml := TOleAuto():New( ::cActivexName+".Xml" )
::oXml:Clear()
RETURN Self
//------------------------------------------------------------------------------
METHOD Close() CLASS TXml
::Free()
RETURN Nil
//------------------------------------------------------------------------------
METHOD Free() CLASS TXml
::oXml := Nil
::Super:Free()
RETURN Nil
//------------------------------------------------------------------------------
//Busca el contenido de un TAG por su path completo con todos sus niveles //OJO ES CASE SENSITIVE
METHOD GetContent(aPath)
Local cContent:="", nItem:=0
If Len(aPath)=0 .Or. ValType(aPath)<>"A"
Return Nil
Endif
For nItem:=1 to Len(aPath)
::oXml:FindChild2(aPath[nItem])
Next
cContent:=::oXml:Content
*MsgInfo(cContent)
For nItem:=1 to Len(aPath)
::oXml:GetParent2()
Next
RETURN cContent
//------------------------------------------------------------------------------
/* Sample
With Object TXml():New( Self )
:Tag("atom:entry")
:AddAttribute("xmlns:atom","http://www.w3.org/2005/Atom")
:AddAttribute("xmlns:gd","http://schemas.google.com/g/2005")
:NewChild("atom:category","")
:AddAttributeAt("atom:category","scheme","http://schemas.google.com/g/2005#kind")
:AddAttributeAt("atom:category","term","http://schemas.google.com/contact/2008#contact")
:NewChild("gd:name|gd:givenName","Bingen")
:NewChild("gd:name|gd:familyName","Ugaldebere")
:NewChild("gd:name|gd:fullName","Bingen Ugaldebere")
:NewChild("atom:content","Observaciones")
:AddAttributeAt("atom:content","type","text")
:NewChild("gd:email","")
:AddAttributeAt("gd:email","primary","true")
:AddAttributeAt("gd:email","displayName","Bingen Ugaldebere")
:AddAttributeAt("gd:email","rel","http://schemas.google.com/g/2005#work")
:AddAttributeAt("gd:email","address","bingen@bisoft.es")
:NewChild("gd:phoneNumber","+34 944029260")
:AddAttributeAt("gd:phoneNumber","primary","true")
:AddAttributeAt("gd:phoneNumber","rel","http://schemas.google.com/g/2005#work")
:NewChild("gd:structuredPostalAddress|gd:city","Bilbao")
:NewChild("gd:structuredPostalAddress|gd:street","Ald. Mazarredo 47")
:NewChild("gd:structuredPostalAddress|gd:region","Bizkaia")
:NewChild("gd:structuredPostalAddress|gd:postcode","48009")
:NewChild("gd:structuredPostalAddress|gd:country","Euskadi")
:AddAttributeAt("gd:structuredPostalAddress","primary","true")
:AddAttributeAt("gd:structuredPostalAddress","rel","http://schemas.google.com/g/2005#work")
cXml:=:GetXml()
End
Show this.......................
<?xml version="1.0" encoding="utf-8" ?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005">
<atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" />
<gd:name>
<gd:givenName>Bingen</gd:givenName>
<gd:familyName>Ugaldebere</gd:familyName>
<gd:fullName>Bingen Ugaldebere</gd:fullName>
</gd:name>
<atom:content type="text">Observaciones</atom:content>
<gd:email primary="true" displayName="Bingen Ugaldebere" rel="http://schemas.google.com/g/2005#work" address="bingen@bisoft.es" />
<gd:phoneNumber primary="true" rel="http://schemas.google.com/g/2005#work">+34 944029260</gd:phoneNumber>
<gd:structuredPostalAddress primary="true" rel="http://schemas.google.com/g/2005#work">
<gd:city>Bilbao</gd:city>
<gd:street>Ald. Mazarredo 47</gd:street>
<gd:region>Bizkaia</gd:region>
<gd:postcode>48009</gd:postcode>
<gd:country>Euskadi</gd:country>
</gd:structuredPostalAddress>
</atom:entry>
*/