Page 1 of 1

read xml file

Posted: Wed Dec 09, 2020 9:20 am
by kajot
How read xml file size 10Mb

cFileName:='test.xml'

I am using

oXmlDoc := TXmlDocument():new( ) // HBXML_TYPE_CDATA HBXML_STYLE_NOESCAPE
IF oXmlDoc:nStatus != HBXML_STATUS_OK
Msginfo( "error file .XML" )
Break
ENDIF


oXmlDoc:read( Memoread( cFileName ) )


best regard
KAJOT

Re: read xml file

Posted: Wed Dec 09, 2020 11:38 am
by Antonio Linares
Kajot,

Please review function OpenXML() source code here:
https://github.com/FiveTechSoft/FWH_too ... er/pim.zip

Code: Select all

function OpenXML( cFileName, oTree )

   local aRoots := {}, hFile, oXmlDoc, oXmlIter, oTagLast, oTagActual, oCol

   hFile    = FOpen( cFileName )
   oXmlDoc  = TXmlDocument():New( hFile )
   oXmlIter = TXmlIterator():New( oXmlDoc:oRoot )

   if oXmlDoc:nError != 0
      MsgStop( "Filename: " + cFileName + CRLF + ;
               "Line " + AllTrim( Str( oXmlDoc:nLine ) ) + CRLF + ;
               "Description: " + XmlError( oXmlDoc:nError ) + CRLF + ;
               "Node: <" + If( ! Empty( oXmlDoc:oErrorNode:cName ), oXmlDoc:oErrorNode:cName, "" ) + ;
               ">", "XML error" )
      return .F.        
   endif

   // XBrowse( oXmlDoc )
    
   AAdd( aRoots, oTree )

   while ( oTagActual := oXmlIter:Next() ) != nil
      // XBROWSER oTagActual ;
      //    SETUP ( oCol := oBrw:AddCol(), oCol:bStrData := { || oTagActual:Depth() }, oCol:cHeader := "Depth" ) 
      
      // MsgInfo( oTagActual:cName )

      if oTagLast != nil
         if oTagLast:Depth() < oTagActual:Depth()
            AAdd( aRoots, nil )
            aRoots[ oTagActual:Depth() + 1 ] = aRoots[ oTagActual:Depth() ]:Add( oTagActual:cName )
            aRoots[ oTagActual:Depth() + 1 ]:Cargo = oTagActual:cData
         endif

         if oTagLast:Depth() > oTagActual:Depth()
            aRoots[ oTagActual:depth() + 1 ] = aRoots[ oTagActual:Depth() ]:Add( oTagActual:cName )
            aRoots[ oTagActual:depth() + 1 ]:Cargo = oTagActual:cData
         endif

         if oTagLast:Depth() == oTagActual:Depth()
            aRoots[ oTagActual:Depth() + 1 ] = aRoots[ Max( oTagLast:Depth(), 1 ) ]:Add( oTagActual:cName )
            aRoots[ oTagActual:Depth() + 1 ]:Cargo = oTagActual:cData                                        
         endif
      else
         AAdd( aRoots, oTree:Add( oTagActual:cName ) )
         ATail( aRoots ):Cargo = oTagActual:cData
      endif
      oTagLast = oTagActual
   end

   FClose( hFile )

return .T.      
 

Re: read xml file

Posted: Thu Dec 10, 2020 4:29 pm
by Marc Venken
Antonio,

#include "c:\harbour\contrib\xhb\hbxml.ch"

Where is this file located ? I think it is needed, because I get a error in testing.

Turbo Incremental Link 6.70 Copyright (c) 1997-2014 Embarcadero Technologies, Inc.
Error: Unresolved external '_HB_FUN_XMLERROR' referenced from C:\PROGRAMMAS\FOTOSELECT\OBJ\TEST1.OBJ
Error: Unable to perform link

** error 2 ** deleting TEST.EXE

Re: read xml file

Posted: Thu Dec 10, 2020 5:56 pm
by Antonio Linares
Marc,

Please include this function in your PRG:

Code: Select all

function XmlError( nError )

   local aErrors := { "HBXML_ERROR_IO", "HBXML_ERROR_NOMEM", "HBXML_ERROR_OUTCHAR",;
                      "HBXML_ERROR_INVNODE", "HBXML_ERROR_INVATT", "HBXML_ERROR_MALFATT",;
                      "HBXML_ERROR_INVCHAR", "HBXML_ERROR_NAMETOOLONG", "HBXML_ERROR_ATTRIBTOOLONG",;
                      "HBXML_ERROR_VALATTOOLONG", "HBXML_ERROR_UNCLOSED", "HBXML_ERROR_UNCLOSEDENTITY",;
                      "HBXML_ERROR_WRONGENTITY" }

return If( nError > 0 .and. nError < 14, aErrors[ nError ], "error range must be 1-13" )
 
Here it is the XML header file:
https://github.com/harbour/core/blob/ma ... b/hbxml.ch

Re: read xml file

Posted: Thu Dec 10, 2020 11:28 pm
by Marc Venken
Thank you.

PIM.EXE is a sample for reading a XML file and editing is also possible, but it is not a process for more options ?
I suppose it is not the idea of creation a PIM.exe that also reads the xml and can put it into a dbf.

That option is what I'm looking at now. I need to download more that once a XML file from my webshop, containing the colors and Hex values used in product pages.

sample file :

<?xml version="1.0"?>
<options version="1">
<item>
<id>1</id>
<sortOrder>127</sortOrder>
<widget>true</widget>
<googleTag/>
<beslistTag/>
<channableTag/>
<name>
<dutch>Kleur</dutch>
</name>
<values>
<item>
<id>2459</id>
<sortOrder>0</sortOrder>
<colorHex>A12839</colorHex>
<name>
<dutch>aardbei</dutch>
</name>
</item>
<item>
<id>2</id>
<sortOrder>0</sortOrder>
<colorHex/>
<name>
<dutch>antraciet</dutch>
</name>
</item>
<item>
<id>2522</id>
<sortOrder>0</sortOrder>
<colorHex/>
<name>
<dutch>antraciet/zwart</dutch>
</name>
</item>
<item>
<id>2675</id>
<sortOrder>0</sortOrder>
<colorHex/>
<name>
<dutch>ANTRACIETGRIJS</dutch>
</name>
</item>
<item>
<id>2564</id>
<sortOrder>0</sortOrder>
<colorHex/>
<name>
<dutch>ZWART&#xF8;</dutch>
</name>
</item>
</values>
</item>
</options>

This function is going the right way in converting to xbrowse data that can be saved later.

Code: Select all

function Xmlreader()
   local aMail:={},nTel:= 1
   local hFile    := FOpen( "c:\programmas\fotoselect\kleuren.xml"  )

   Local oXmlDoc  := TXmlDocument():New( hFile )
   Local oXmlIter := TXmlIterator():New( oXmlDoc:oRoot ), oTagActual
   while .T.
      oTagActual = oXmlIter:Next()
      If oTagActual != nil
         if oTagActual:cName = "item"
            aadd(aMail,oTagActual:cData)
         endif
         if oTagActual:cName = "dutch"
            aadd(aMail,oTagActual:cData)
         endif
         if oTagActual:cName = "colorHex"
            aadd(aMail,oTagActual:cData)
         endif
//         HEval( oTagActual:aAttributes, { | cKey, cValue | MsgInfo( cKey, cValue ) } )
      Else
         Exit
      Endif
      if nTel > 5
         exit
      endif

   End
   xbrowse(aMail)
   FClose( hFile )

return nil

 
Data that I become =


A

Kleur

A12839
aardbei


antraciet


antraciet/zwart


ANTRACIETGRIJS


ANTRACIETGRIJS/AZUURBLAUW


ANTRACIETGRIJS/ZWART

00A0DF
aqua


Army Groen/Zwart

06638f
Assure


Azure


AZUURBLAUW


AZUURBLAUW/ANTRACIETGRIJS


AZUURBLAUW/ZWART

A0BAD3
babyblauw

ECC9E9
babyrose

// end data sample -------------------------------------------------------------------------------------------------------------------

In fact i finaly would like this in a database:

ID ColorHex Name
2459 A12839 aardbei
2 antraciet
....
83 06638f Assure

Is a XML file build always the same way ?

Re: read xml file

Posted: Fri Dec 11, 2020 9:01 am
by Antonio Linares
Marc,

pim.prg is just an example using FWH, XML, TreeViews, RichEdit, etc. Still there are some bugs to fix.

This is a modified version of your XML that opens fine using pim.exe:

marc.xml
<xml>version="1.0"
<options>
<values>
<item>
<id>1</id>
<sortOrder>127</sortOrder>
<widget>true</widget>
<name>
<dutch>Kleur</dutch>
</name>
</item>
<item>
<id>2459</id>
<sortOrder>0</sortOrder>
<colorHex>A12839</colorHex>
<name>
<dutch>aardbei</dutch>
</name>
</item>
<item>
<id>2</id>
<sortOrder>0</sortOrder>
<colorHex/>
<name>
<dutch>antraciet</dutch>
</name>
</item>
<item>
<id>2522</id>
<sortOrder>0</sortOrder>
<colorHex/>
<name>
<dutch>antraciet/zwart</dutch>
</name>
</item>
<item>
<id>2675</id>
<sortOrder>0</sortOrder>
<colorHex/>
<name>
<dutch>ANTRACIETGRIJS</dutch>
</name>
</item>
<item>
<id>2564</id>
<sortOrder>0</sortOrder>
<colorHex/>
<name>
<dutch>ZWART&#xF8;</dutch>
</name>
</item>
</values>
</options>
</xml>

Re: read xml file

Posted: Tue Dec 22, 2020 10:52 am
by Antonio Linares
Fixed bug when saving nested childs:

Full source code and EXE:
https://github.com/FiveTechSoft/FWH_too ... er/pim.zip