read xml file

read xml file

Postby kajot » Wed Dec 09, 2020 9:20 am

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
best regards
kajot
User avatar
kajot
 
Posts: 339
Joined: Thu Nov 02, 2006 6:53 pm
Location: Poland

Re: read xml file

Postby Antonio Linares » Wed Dec 09, 2020 11:38 am

Kajot,

Please review function OpenXML() source code here:
https://github.com/FiveTechSoft/FWH_tools/blob/master/pim.zip

Code: Select all  Expand view
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.      
 
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42082
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: read xml file

Postby Marc Venken » Thu Dec 10, 2020 4:29 pm

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
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1429
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: read xml file

Postby Antonio Linares » Thu Dec 10, 2020 5:56 pm

Marc,

Please include this function in your PRG:
Code: Select all  Expand view
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/master/contrib/xhb/hbxml.ch
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42082
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: read xml file

Postby Marc Venken » Thu Dec 10, 2020 11:28 pm

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  Expand view

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 ?
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1429
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: read xml file

Postby Antonio Linares » Fri Dec 11, 2020 9:01 am

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>
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42082
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: read xml file

Postby Antonio Linares » Tue Dec 22, 2020 10:52 am

Fixed bug when saving nested childs:

Full source code and EXE:
https://github.com/FiveTechSoft/FWH_tools/blob/master/pim.zip
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42082
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 59 guests