Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
XmlAdapter and schema
Message
De
16/04/2003 03:03:50
 
 
À
15/04/2003 04:17:22
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00777588
Message ID:
00778196
Vues:
48
>The general question about xmladaptor is can i convert any xml document to table(s) by using certain scemas or these xmls should obey to certain rules ??
>

Hi Dimitris,

XMLAdapter understands several types of XML schemas, it is all should be described in help. When XML schema is missing or is not supported, XMLAdapter doesn't try to infer the schema, but you can describe the schema to XMLAdapter by adding XMLTable object(s) into XMLAdapter.Tables collection. Basically, XMLAdapter can deal with any XML document if its schema can be described using XMLTable objects.

Below is an example how to make XMLAdapter to understand XML like this:
<?xml version="1.0" encoding="utf-8" ?> 
<root>
	<Header Sender='CompanyA' Recipient='CompanyB'/>
	<Detail Trailer_ID='3545452355454234' Lorry_ID='1523445532'/>
	<Detail Trailer_ID='4444442355454234' Lorry_ID='5555545532'/>
</root>	
CLOSE DATABASES all
clear

TEXT TO cXml NOSHOW
<?xml version="1.0" encoding="utf-8" ?> 
<root>
	<Header Sender='CompanyA' Recipient='CompanyB'/>
	<Detail Trailer_ID='3545452355454234' Lorry_ID='1523445532'/>
	<Detail Trailer_ID='4444442355454234' Lorry_ID='5555545532'/>
</root>	
ENDTEXT

LOCAL oXA as XMLAdapter, oXT as XMLTable, oXF as XMLField

oXA=CREATEOBJECT("XMLAdapter")
oXA.LoadXML(cXml)

* create XMLTable object for the first table
oXT=CREATEOBJECT("XMLTable")
oXT.XMLName=STRCONV("Header",5)
oXT.Alias="Header"
oXA.Tables.Add(oXT,oXT.XMLName)

oXF=CREATEOBJECT("XMLField")
oXF.XMLName=STRCONV("Sender",5)
oXF.IsAttribute= .T.
oXF.Alias="Sender"
oXF.DataType="C"
oXF.MaxLength=10
oXT.Fields.Add(oXF,oXF.XMLName)

oXF=CREATEOBJECT("XMLField")
oXF.XMLName=STRCONV("Recipient",5)
oXF.IsAttribute= .T.
oXF.Alias="Recipient"
oXF.DataType="C"
oXF.MaxLength=10
oXT.Fields.Add(oXF,oXF.XMLName)

* create XMLTable object for the second table
oXT=CREATEOBJECT("XMLTable")
oXT.XMLName=STRCONV("Detail",5)
oXT.Alias="Detail"
oXA.Tables.Add(oXT,oXT.XMLName)

oXF=CREATEOBJECT("XMLField")
oXF.XMLName=STRCONV("Trailer_ID",5)
oXF.IsAttribute= .T.
oXF.Alias="Trailer_ID"
oXF.DataType="C"
oXF.MaxLength=20
oXT.Fields.Add(oXF,oXF.XMLName)

oXF=CREATEOBJECT("XMLField")
oXF.XMLName=STRCONV("Lorry_ID",5)
oXF.IsAttribute= .T.
oXF.Alias="Lorry_ID"
oXF.DataType="C"
oXF.MaxLength=10
oXT.Fields.Add(oXF,oXF.XMLName)

FOR EACH oXT IN oXA.Tables
	?oXT.ToCursor()
	SELECT (oXT.Alias)
	?ALIAS()
	LIST
NEXT

return
The same code will work for XML like this.
<?xml version="1.0" encoding="utf-8" ?> 
<root>
	<Header Sender='CompanyA' Recipient='CompanyB'>
		<Detail Trailer_ID='3545452355454234' Lorry_ID='1523445532'/>
		<Detail Trailer_ID='4444442355454234' Lorry_ID='5555545532'/>
	</Header>	
</root>	
But with this XML you can instruct XMLAdapter to join these tables by using XMLTable.ChildTable property:
CLOSE DATABASES all
clear

TEXT TO cXml NOSHOW
<?xml version="1.0" encoding="utf-8" ?> 
<root>
	<Header Sender='CompanyA' Recipient='CompanyB'>
		<Detail Trailer_ID='3545452355454234' Lorry_ID='1523445532'/>
		<Detail Trailer_ID='4444442355454234' Lorry_ID='5555545532'/>
	</Header>	
</root>	
ENDTEXT

LOCAL oXA as XMLAdapter, oXT as XMLTable, oXF as XMLField

oXA=CREATEOBJECT("XMLAdapter")
oXA.LoadXML(cXml)

* create XMLTable object for the first table
oXT=CREATEOBJECT("XMLTable")
oXT.XMLName=STRCONV("Header",5)
oXA.Tables.Add(oXT,oXT.XMLName)

oXF=CREATEOBJECT("XMLField")
oXF.XMLName=STRCONV("Sender",5)
oXF.IsAttribute= .T.
oXF.Alias="Sender"
oXF.DataType="C"
oXF.MaxLength=10
oXT.Fields.Add(oXF,oXF.XMLName)

oXF=CREATEOBJECT("XMLField")
oXF.XMLName=STRCONV("Recipient",5)
oXF.IsAttribute= .T.
oXF.Alias="Recipient"
oXF.DataType="C"
oXF.MaxLength=10
oXT.Fields.Add(oXF,oXF.XMLName)

* create XMLTable object for the second table
oXT=CREATEOBJECT("XMLTable")
oXT.XMLName=STRCONV("Detail",5)

oXA.Tables(1).ChildTable=oXT && <== this is the interesting part

oXF=CREATEOBJECT("XMLField")
oXF.XMLName=STRCONV("Trailer_ID",5)
oXF.IsAttribute= .T.
oXF.Alias="Trailer_ID"
oXF.DataType="C"
oXF.MaxLength=20
oXT.Fields.Add(oXF,oXF.XMLName)

oXF=CREATEOBJECT("XMLField")
oXF.XMLName=STRCONV("Lorry_ID",5)
oXF.IsAttribute= .T.
oXF.Alias="Lorry_ID"
oXF.DataType="C"
oXF.MaxLength=10
oXT.Fields.Add(oXF,oXF.XMLName)

?oXA.Tables(1).ToCursor(.F.,"Header_Detail")
SELECT Header_Detail
?ALIAS()
LIST

return
Detail could also have a ChildTable. There are many interesting things you can do with XMLAdapter.

Thanks,
Aleksey.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform