Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Cetin Basoz
  Where is Cetin Basoz?
 Izmir
 Turkey
 Cetin Basoz
 To: Paul Gibson
  Where is Paul Gibson?
 Glasgow
 United Kingdom
 Paul Gibson
 Tags
Subject: RE: Efficient XML Parsing
Thread ID: 372993 Message ID: 373262 # Views: 107 # Ratings: 3
Version: Visual FoxPro 9 SP2 Category: XML
Date: Wednesday, March 20, 2013 12:52:49 PM         
   


Paul,
Here are some XMLAdapter code that might help:

CreateNestedXML('d:\temp\mynested.xml')
ReadNestedXML('d:\temp\mynested.xml')

Procedure CreateNestedXML(tcFileName)
	Use _samples+"\data\customer" In 0
	Use _samples+"\data\orders" In 0 Order Tag cust_id
	Use _samples+"\data\orditems" In 0 Order Tag order_id
	Use _samples+"\data\employee" In 0 Order Tag emp_id
	Set Relation To order_id Into 'orditems' In 'orders'
	Set Relation To cust_id Into 'orders' In 'customer'
	Set Relation To emp_id Into 'employee' In 'orders' Additive

	Local oAdapter As Xmladapter
	oAdapter = Createobject('xmlAdapter')
	oAdapter.RespectNesting = .T.
	oAdapter.AddTableSchema('customer',.F.,Strconv('Customer',12))
	oAdapter.AddTableSchema('orders',.F.,Strconv('OrdersOfCustomer',12))
	oAdapter.AddTableSchema('orditems',.F.,Strconv('OrderDetails',12))
	oAdapter.AddTableSchema('employee',.F.,Strconv('SalesPerson',12))
	oAdapter.ToXML(m.tcFileName,'',.T.)

	Use In (Select('customer'))
	Use In (Select('orders'))
	Use In (Select('ordItems'))
	Use In (Select('employee'))
Endproc

Procedure ReadNestedXML(tcFileName)
	Local oAdapter As Xmladapter, ix
	oAdapter = Createobject('xmlAdapter')
	oAdapter.RespectNesting = .T.
	oAdapter.LoadXML(m.tcFileName,.T.,.T.)

	For ix= 1 To oAdapter.Tables.Count
		oAdapter.Tables.Item(m.ix).ToCursor()
		Browse
		use
	Endfor
endproc



For next sample I don't remember where I got the original XML to parse but anyway demonstrates parsing an XML that VFP doesn't do straightforward and needs some help (I think it was Tsingau from VFP team who knew these XML thing best - kill me I can't remember his exact name):

*****WARNING: It is likely XML gets rendered here and you might need to get it in reply mode.***

Close Databases All
Local lcXML
TEXT TO lcXML noshow
<?xml version="1.0" encoding="iso-8859-9"?>
<ROOT>
<Stok AnaGrup_Kod="S03" AltGrup_Kod="S0310" Kod="bcdoem_0027">
            <STOKOZELLIKLER>
            <OZELLIKLER OZELLIK="xx" DEGER="50 lik pakerlerde"/>
            <OZELLIKLER OZELLIK="yy" DEGER="CD-R KAYIT OZELLIGI"/>
            </STOKOZELLIKLER>
    </Stok>
  <Stok AnaGrup_Kod="S03" AltGrup_Kod="S0310" Kod="bcdv_0005">
   </Stok>
    <Stok AnaGrup_Kod="S03" AltGrup_Kod="S0310" Kod="bcdv_0024">
            <STOKOZELLIKLER>
            <OZELLIKLER OZELLIK="xx1" DEGER="50 lik pakerlerde"/>
            <OZELLIKLER OZELLIK="yy2" DEGER="CD-R KAYIT OZELLIGI"/>
            <OZELLIKLER OZELLIK="yy3" DEGER="Falan filan"/>
            <OZELLIKLER OZELLIK="yy4" DEGER=""/>
            </STOKOZELLIKLER>
    </Stok>
</ROOT>
ENDTEXT
Local loAdapter As Xmladapter, ;
  loHelper, ;
  t1 As Xmltable, t2 As Xmltable, t3 As Xmltable
loHelper = Createobject('XMLAdapterUtils')

loAdapter = Createobject('XMLAdapter')
loAdapter.LoadXML( m.lcXML )

t1 = loHelper.MakeTable("Stok")
t2 = loHelper.MakeTable("STOKOZELLIKLER")
t3 = loHelper.MakeTable("OZELLIKLER")

t1.ChildTable = m.t2
t2.ChildTable = m.t3
loAdapter.Tables.Add(m.t1, m.t1.XMLName)

loHelper.MakeField(m.t1, "@AnaGrup_Kod", "AnaGrup_Kod", "C",  3,0,.T.)
loHelper.MakeField(m.t1, "@AltGrup_Kod", "AltGrup_Kod", "C",  5,0,.T.)
loHelper.MakeField(m.t1, "@Kod",         "Kod",         "C", 40,0,.T.)

loHelper.MakeField(m.t3, "@OZELLIK","OZELLIK",     "C",100,0,.F.)
loHelper.MakeField(m.t3, "@DEGER","DEGER",       "C",100,0,.F.)

loAdapter.Tables(1).ToCursor()

Define Class XMLAdapterUtils As Custom
  Procedure MakeTable(tcTableName)
    Local loTable As Xmltable
    loTable=Createobject("XMLTable")
    loTable.XMLName = Strconv(m.tcTableName,5)
    loTable.Alias=m.tcTableName
    Return loTable
  Endproc
  Procedure MakeField(toTable,tcXMLName, tcFieldName,tcFieldType,tnFieldLen,tnDecimals,tlAttribute)
    Local loField As Xmlfield
    loField=Createobject("XMLField")
    loField.XMLName=Strconv(m.tcXMLName,5)
    loField.Alias=Evl(m.tcFieldName,m.tcXMLName)
    loField.XMLNameIsXpath = .T.
    loField.DataType=m.tcFieldType
    loField.MaxLength=m.tnFieldLen
    If Evl(m.tnDecimals,0) > 0
      loField.FractionDigits = m.tnDecimals
    Endif
    toTable.Fields.Add(loField,loField.XMLName)
  Endproc
Enddefine





Cetin Basoz

.Net has got better.Think about moving - check my blog:
My Blog


MongoDb Certified Developer
MongoDb Certified DBA


Support Wikipedia

ENTIRE THREAD

Efficient XML Parsing Posted by Paul Gibson @ 3/15/2013 4:05:13 PM
RE: Efficient XML Parsing Posted by James Frye @ 3/15/2013 7:21:49 PM
RE: Efficient XML Parsing Posted by Marco Plaza @ 3/15/2013 7:44:46 PM
RE: Efficient XML Parsing Posted by Ulhas Amrite @ 3/31/2013 5:43:14 AM
RE: Efficient XML Parsing Posted by Marco Plaza @ 3/31/2013 5:08:50 PM
RE: Efficient XML Parsing Posted by Tamar Granor @ 3/15/2013 9:14:02 PM
RE: Efficient XML Parsing Posted by Rick Hodgin @ 3/15/2013 9:29:53 PM
RE: Efficient XML Parsing Posted by Rick Hodgin @ 3/15/2013 9:47:44 PM
RE: Efficient XML Parsing Posted by Cetin Basoz @ 3/15/2013 11:38:43 PM
RE: Efficient XML Parsing Posted by Srdjan Djordjevic @ 3/19/2013 5:24:20 PM
RE: Efficient XML Parsing Posted by Cetin Basoz @ 3/20/2013 12:47:51 PM
RE: Efficient XML Parsing Posted by Srdjan Djordjevic @ 5/14/2013 11:35:15 AM
RE: Efficient XML Parsing Posted by Cetin Basoz @ 3/20/2013 12:52:49 PM
RE: Efficient XML Parsing Posted by Paul Gibson @ 3/20/2013 3:37:02 PM
RE: Efficient XML Parsing Posted by Cetin Basoz @ 3/20/2013 6:57:31 PM
RE: Efficient XML Parsing Posted by Marco Plaza @ 3/21/2013 5:06:20 AM
RE: Efficient XML Parsing Posted by Cetin Basoz @ 3/21/2013 2:15:33 PM
RE: Efficient XML Parsing Posted by Marco Plaza @ 3/21/2013 6:28:05 PM
RE: Efficient XML Parsing Posted by Paul Gibson @ 3/21/2013 1:31:38 PM
RE: Efficient XML Parsing Posted by Cetin Basoz @ 3/21/2013 2:50:30 PM
RE: Efficient XML Parsing Posted by Tamar Granor @ 3/20/2013 9:30:55 PM
RE: Efficient XML Parsing Posted by Paul Gibson @ 3/21/2013 1:35:12 PM
RE: Efficient XML Parsing Posted by Tamar Granor @ 3/22/2013 9:25:46 PM
RE: Efficient XML Parsing Posted by James Frye @ 5/11/2013 4:29:23 PM
RE: Efficient XML Parsing Posted by Russell Hill @ 3/21/2013 2:25:06 AM
RE: Efficient XML Parsing Posted by Paul Gibson @ 3/21/2013 1:34:45 PM
RE: Efficient XML Parsing Posted by Russell Hill @ 3/21/2013 10:19:56 PM
RE: Efficient XML Parsing Posted by Paul Gibson @ 5/8/2013 3:36:24 PM
RE: Efficient XML Parsing Posted by Russell Hill @ 5/9/2013 1:07:22 AM
RE: Efficient XML Parsing Posted by Paul Gibson @ 5/9/2013 2:01:18 PM
RE: Efficient XML Parsing Posted by Russell Hill @ 5/9/2013 2:29:08 PM
RE: Efficient XML Parsing Posted by Paul Gibson @ 5/15/2013 12:27:19 AM
RE: Efficient XML Parsing Posted by Russell Hill @ 5/15/2013 1:11:27 AM
RE: Efficient XML Parsing Posted by Cetin Basoz @ 5/9/2013 2:54:01 PM
RE: Efficient XML Parsing Posted by Russell Hill @ 5/10/2013 1:23:41 AM
RE: Efficient XML Parsing Posted by Cetin Basoz @ 5/10/2013 3:01:55 AM
RE: Efficient XML Parsing Posted by Russell Hill @ 5/10/2013 3:10:19 AM
RE: Efficient XML Parsing Posted by Cetin Basoz @ 5/10/2013 1:09:59 PM
RE: Efficient XML Parsing Posted by Rick Hodgin @ 5/10/2013 3:49:46 PM
RE: Efficient XML Parsing Posted by Cetin Basoz @ 3/21/2013 2:22:38 PM
RE: Efficient XML Parsing Posted by Russell Hill @ 3/21/2013 10:47:28 PM
RE: Efficient XML Parsing Posted by Jose Mendes @ 3/28/2013 8:12:46 AM
RE: Efficient XML Parsing Posted by Russell Hill @ 3/29/2013 11:57:43 AM
RE: Efficient XML Parsing Posted by Jose Mendes @ 3/30/2013 6:43:29 PM
RE: Efficient XML Parsing Posted by Marco Plaza @ 3/29/2013 7:27:28 PM
RE: Efficient XML Parsing Posted by Jose Mendes @ 3/30/2013 6:44:07 PM