Reading complex XML file

1,098 views
Skip to first unread message

d s

unread,
Jul 27, 2021, 2:11:24 AM7/27/21
to Harbour Users

As I need to read not quite simple XML file with invoice data, in this forum found suggestions to use TXML classes, with example in harbour/contrib/xhb/tests\xml1.prg.

That example uses realtivelly simple test.xml file to read. My XML invoice data are more complex, have several  elements with deep structure, for example data about supplier, or data about invoice lines.

I do not know how to read nodes with deep structure inside root element.

I am sending file with some reduced example of my XML invoice data, based on UBL 2.1 format.

Is there are any list of possible TXML methods I can  use in my PRG in order to successfully read my XML data ?

I have tried using xml1.prg as starting point, but stacked on complex nodes like are <cac:AccountingSupplierParty> or <cac:AccountingCustomerParty> and <cac:InvoiceLine>.  If anybody knows how to do it please suggest how to read that kind of nodes.

Thanks in advance and Best regards.

Simo.

Invoice.xml

Shaji Thomas

unread,
Jul 27, 2021, 6:37:13 AM7/27/21
to Harbour Users
Hi,

See the conversation on  this subject  on 04/09/2020 ...

"Hb_JsonEncode() and hb_JsonDecode() are Harbour functions. hb_xmlToHash() and hb_hashToXML() are developed by Przemek and were published in this group many years back." 

d s

unread,
Jul 27, 2021, 7:32:10 AM7/27/21
to Harbour Users
Many thanks Thomas ! 
I will read suggested conversations.
Meanwhile I am progressing in using TXML classes. If I find solution, I will inform what I did.
Best regards,
Simo.

M., Ronaldo

unread,
Aug 2, 2021, 1:37:05 AM8/2/21
to Harbour Users
Your file don't meet XML standards ! But it can be solved.


Solution 1:Using Harbour
...
   cTxt := hb_MemoRead( cFile )
   cTxt := StrTran(  cTxt , " <Invoice> ", " <Invoice xmlns:cbc="http://www.google.com" xmlns:cac="http://www.google.com"> " )
   hb_MemoWrit(  cFile, cTxt )
...


Solution 2:Open it with any text editor and change 2nd line
From <Invoice>
To <Invoice xmlns:cbc="http://www.google.com" xmlns:cac="http://www.google.com">

M., Ronaldo

unread,
Aug 2, 2021, 1:42:41 AM8/2/21
to Harbour Users
Use hbmxml to read and write XML files.

See samples in folder: \contrib\hbmxml\tests

*TXML will cause memory leak and broke your code
On Tuesday, July 27, 2021 at 3:11:24 AM UTC-3 cod...@outlook.com wrote:

d s

unread,
Aug 2, 2021, 1:51:25 AM8/2/21
to Harbour Users
Thank you Ronaldo !
Meanwhile I began to try TXML. I made some progress.
As it has memory leak problem, I will read \contrib\hbmxml\test.
Best regards.
Simo.

d s

unread,
Aug 20, 2021, 2:57:37 PM8/20/21
to Harbour Users
Here is what I did.

I decided to use TXML. I saw it as most convenient  to me. In \harbour\contrib\xhb\tests found xml1.prg, and used it as example how to read my XML file.

XML file I need to read is complex. It contains data about invoices, formatted according to UBL 2.1 (European Universal Business Language, version 2.1).
As attachment is example PRG how I read that file using TXML. My goal was to read every data node (for now without attributes), its value, and its full path i invoice XML structure.
These data I quite enough to do other desired processing on data elements. 
May be this will help other  who need to read UBL XML file.

Best regards,
Simo.

txmlread.prg

M., Ronaldo

unread,
Aug 25, 2021, 11:14:36 AM8/25/21
to Harbour Users
mxml
An small sample to read XML files

read_invoice.prg
invoice.xml

d s

unread,
Aug 25, 2021, 12:49:37 PM8/25/21
to Harbour Users
Thank you Ronaldo.
 
Your example will help us to better understand how to use  MXML library. 

Best  regards.

Simo.
Message has been deleted
Message has been deleted

M., Ronaldo

unread,
Jan 15, 2022, 6:55:38 PM1/15/22
to Harbour Users
   // *---------------------------------------------------------------------*
   // mr_MXml
   //
   // A simple class to read a complex XML file
   //
   // hbmk2.exe demo.hbp
   // *---------------------------------------------------------------------*
   WITH OBJECT mr_MXml():New( 'invoice.xml' )

      :create()

          :SetNode( 'invoice' )
          ? :GetStr( 'uid' )
          ? :GetStr( 'mark' )

          :SetNode( 'invoice', 'invoiceDetails' )
          ? :GetStr( 'netValue' )
          ? :GetStr( 'vatCategory' )
          ? :GetStr( 'vatAmount' )

          :close()

   END WITH
invoice.xml
mr_MXml.prg
demo.hbp
Demo.prg
readme.txt

Pete

unread,
Jan 16, 2022, 8:42:07 AM1/16/22
to Harbour Users
Useful code.
Thanks for posting!

regards,
Pete
Reply all
Reply to author
Forward
0 new messages