FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Read and Write XML file?
Posts: 1598
Joined: Fri Oct 07, 2005 05:56 PM
Read and Write XML file?
Posted: Fri Mar 23, 2012 04:32 AM

Hi,

Have anybody know, how to read and write XML file?

Any idea, most appreciate.
Dutch

Regards,

Dutch



FWH 2304 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio

FWPPC 10.02 / Harbour for PPC (FTDN)

ADS V.9 / MySql / MariaDB

R&R 12 Infinity / Crystal Report XI R2

(Thailand)
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Read and Write XML file?
Posted: Fri Mar 23, 2012 09:08 AM
Try the following sample to read Xml File contents
Code (fw): Select all Collapse
#include "FiveWin.ch"
#include "hbxml.ch"

//--------------------//
FUNCTION Main()

    LOCAL hFile, cFileName:=""
    LOCAL oXmlDoc, oXmlIter , oXmlNode, cFieldName, cFieldData, cMemo:=""
    
    cFileName := cGetFile( "XML File (*.Xml)|*.Xml|","Select the required XML file ",curdir())
    
    If !file(cFileName)
        Return nil
    Endif
    
    hFile := FOpen( cFileName )
    
    oXmlDoc := TXmlDocument():New( hFile )
    
    IF oXmlDoc:nStatus != HBXML_STATUS_OK
        Msginfo("Error reading XML ")
        RETURN
    ENDIF
    
    oXmlIter := TXmlIterator():New( oXmlDoc:oRoot )
    oXmlNode := oXmlIter:Find()
    
    // Dumping XML Data to cMemo, Creating header
    cMemo:=Padr("FieldName",25)+Padr("FieldData",30)+CRLF
    cMemo+=Replicate("-",55)+CRLF
    
    DO WHILE oXmlNode != NIL
    
        if !empty(oXmlNode:cData)
        
            if subs(oXmlNode:cData,1,1) # "<"
                cFieldName :=oXmlNode:cName
                cFieldData:=oXmlNode:cData
            Endif
        
        else
            cFieldName := oXmlNode:cName
        endif

        oXmlNode := oXmlIter:Next() // Proceed to the next field

        // Dumpix xml data to cMemo
        cMemo += Padr(cFieldName,25)+cFieldData+CRLF        
    ENDDO
    MemoWrit("YourXMLTxt.Txt",cMemo)
    
    // Display the contents of the newly created text file
    WinExec("notepad.exe YourXMLTxt.Txt")

Return nil


Are you using an ADO Recordset ?. If so, you can easily convert your data to xml format by using the following command

Code (fw): Select all Collapse
oRecordset:Save( "E:\MyAppFolder\MyTest.XML", adPersistXML )


Regards
Anser

Continue the discussion