Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Processing an XML file to VFP cursors
Message
From
19/05/2004 03:18:56
 
General information
Forum:
Visual FoxPro
Category:
XML, XSD
Miscellaneous
Thread ID:
00904549
Message ID:
00905172
Views:
44
>Hi Jamie,
>
>Thank you for your response. I am using VFP 8 and I tried to use the following .
>
>However I am having a problem since loXMLAdapter.Tables.Count = 0 below, so therefore I get an error when I get to the next line. Am I doing something wrong here ??
>
>LOCAL loXMLAdapter as XMLAdapter, loTable as XMLTable
>loXMLAdapter=CREATEOBJECT("XMLAdapter")
>loXMLAdapter.LoadXML('Customers.xml',.T.)
>=MESSAGEBOX(STR(loXMLAdapter.Tables.Count),16,"TABLECOUNT")
>loTable=loXMLAdapter.Tables.Item(1)
>loTable.ToCursor()
>
>Carmel

Hi Carmel,

If there is only one customer element then it is a very simple task for XMLAdapter. First of all, the XML document doesn't include schema, therefore the Tables collection has to be populated manually. In this particular case, all fields are elements and element names are in low case. So, it is just a matter of creating destination cursors and calling AddTableSchema for each of them. If there were many customers or field element names were using upper case letters it would still be possible to use XMLAdapter, but it would require more coding.

Here is a code example. To keep it simple, I didn't create all cursors and fields.
CLOSE DATABASES all
CLEAR

CREATE CURSOR customer ;
(;
customer_number c(6),;
address_id I,;
name c(20);
)

CREATE CURSOR sales ;
(;
name c(20),;
email c(20),;
price_profile c(10),;
price_profile_email c(50);
)

CREATE CURSOR billing;
(;
address_id I,;
title c(20),;
firstname c(20),;
lastname c(20),;
company c(30);
)

LOCAL oXA as XMLAdapter, oXT as XMLTable

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

oXA.AddTableSchema("customer")
oXA.AddTableSchema("sales")
oXA.AddTableSchema("billing")

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

FUNCTION GetXML()

TEXT TO cXML NOSHOW
<?xml version="1.0" encoding="UTF-8"?>
<export_customers_response>
  <authentication>
    <revision>2.0</revision>
    <reseller_shortcut>eric</reseller_shortcut>
    <user_email>test@test.com</user_email>
  </authentication>
  <customers>
    <customer>
      <customer_number>AC0127</customer_number>
      <address_id>626</address_id>
      <name>acme corp</name>
      <address1>1234 lane ave</address1>
      <address2 />
      <address_line1>1234 lane ave</address_line1>
      <address_line2 />
      <city>powell</city>
      <state>Ohio</state>
      <state_abbrv>OH</state_abbrv>
      <zip>43065</zip>
      <country>United States</country>
      <country_abbrv>USA</country_abbrv>
      <phone />
      <fax />
      <website />
      <lead_source />
      <tax_exempt />
      <tax_id />
      <comments />
      <terms>No Terms</terms>
      <credit_limit />
      <sales>
        <name>Lundin, Eric</name>
        <email>eric.lundin@cnet.com</email>
        <price_profile>22% Mark-Up</price_profile>
        <price_profile_email>channelsupport@cnet.com</price_profile_email>
      </sales>
      <billing>
        <address_id>627</address_id>
        <title />
        <firstname>pat</firstname>
        <lastname>leneghan</lastname>
        <company>acme corp</company>
        <address1>1234 lane ave</address1>
        <address2 />
        <city>powell</city>
        <state>Ohio</state>
        <state_abbrv>OH</state_abbrv>
        <zip>43065</zip>
        <country>United States</country>
        <phone />
        <extension />
      </billing>
      <shipping>
        <address_id>627</address_id>
        <title />
        <firstname>pat</firstname>
        <lastname>leneghan</lastname>
        <company>acme corp</company>
        <address1>1234 lane ave</address1>
        <address2 />
        <city>powell</city>
        <state>Ohio</state>
        <state_abbrv>OH</state_abbrv>
        <zip>43065</zip>
        <country>United States</country>
        <phone />
        <extension />
      </shipping>
      <contact>
        <address_id>166722</address_id>
        <title />
        <firstname>pat</firstname>
        <lastname>leneghan</lastname>
        <company>acme corp</company>
        <address1>1234 lane ave</address1>
        <address2 />
        <city>powell</city>
        <state>Ohio</state>
        <state_abbrv>OH</state_abbrv>
        <zip>43065</zip>
        <country>United States</country>
        <phone />
        <extension />
        <home_phone />
        <home_extension />
        <fax />
        <fax_extension />
        <email>pleneghan@aol.com</email>
        <mobile />
        <mobile_extension />
        <pager />
        <website />
        <other />
        <other_extension />
      </contact>
    </customer>
  </customers>
</export_customers_response>
ENDTEXT 

RETURN cXML
ENDFUNC
Here is the output I am getting:
CUSTOMER
Record#  CUSTOMER_NUMBER  ADDRESS_ID NAME                
      1  AC0127                  626 acme corp           

SALES
Record#  NAME                 EMAIL                PRICE_PROFILE PRICE_PROFILE_EMAIL
      1  Lundin, Eric         eric.lundin@cnet.com 22% Mark-U    channelsupport@cnet.com

BILLING
Record#   ADDRESS_ID TITLE                FIRSTNAME            LASTNAME             COMPANY
      1          627                      pat                  leneghan             acme corp
Thanks,
Aleksey.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform