Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help. Using XMLAdapter class in VFP9

Status
Not open for further replies.

nathj

Programmer
Oct 11, 2004
33
GB
Hi,

I am fairly new to the use of XML in FoxPro having only just got my hands on VFP9. I am now developing an application that will take an XML file convert to a VFP data set so that I can update the underlying data teir of the main application.

I will include the XML, the XSD and the code I am usingat the end of this post. The problem I have is that the resultant cursor only contains columns for the elements in the dochdr section of the XML and these are empty - no data in them at all.

CODE
Code:
LOCAL oXMLAdapter AS XMLADAPTER
oXMLAdapter = NEWOBJECT('XMLAdapter')
oXMLAdapter.RespectNesting = .T.
oXMLAdapter.XMLSCHEMALOCATION = "C:\Projects\Online Updates\EasyLink\Sample Data\finobj.xsd"
oXMLAdapter.LOADXML("C:\Projects\Online Updates\EasyLink\Sample Data\ELTestFile.xml", .T.)
IF oXMLAdapter.TABLES.ITEM(1).FIELDS.ITEM(1).DATATYPE <> "M" THEN
 ? 'Failed'
ELSE
 oXMLAdapter.TABLES.ITEM(1).TOCURSOR(.F., "resultXML")
 oXMLAdapter.XMLNAMESPACE=""
 oXMLAdapter.RELEASEXML(.F.)
ENDIF

XML
Code:
<?xml version='1.0' encoding='utf-8'?>
<xmit>
 <dochdr>
  <application>COMTFR</application> 
  <userid>test mailbox</userid> 
  <doctitle>COMMISSION TRANSFER</doctitle> 
  <tranref /> 
  <trandate>22/11/2005</trandate> 
  <trantime>01:35</trantime> 
 </dochdr>
  <senderid>Scotish</senderid> 
  <userid>test mailbox</userid> 
  <icn>85</icn> 
  <apprefno>COMTFR</apprefno> 
  <test /> 
 <doc>
  <brokcode>115555</brokcode> 
  <inscode>165555</inscode> 
 <info>
  <paydate>19/11/2005</paydate> 
  <agencyref>28460</agencyref> 
  <ifacode>115555</ifacode> 
 </info>
 <policy>
  <category>INITIAL</category> 
  <seq>1</seq> 
  <ifaref /> 
  <polnum>4851234</polnum> 
  <schemenum>75999</schemenum> 
  <contype>GP-LINK</contype> 
  <polhold>SMITH-JONES ML</polhold> 
   <comlines>
    <comln>
     <comseq>1</comseq> 
     <commdate>24/10/05</commdate> 
     <freq>M</freq> 
     <pamt /> 
     <comtype>Y</comtype> 
     <camt>-2.02</camt> 
     <currency>GBP</currency> 
     <reascd1>99</reascd1> 
     <reascd2>25</reascd2> 
    </comln>
   </comlines>
  </policy>
 <policy>
  <category>INITIAL</category> 
  <seq>2</seq> 
  <ifaref /> 
  <polnum>4861234</polnum> 
  <schemenum>75999</schemenum> 
  <contype>GP-LINK</contype> 
  <polhold>SMITH-JONES L</polhold> 
   <comlines>
    <comln>
     <comseq>1</comseq> 
     <commdate>24/10/05</commdate> 
     <freq>M</freq> 
     <pamt /> 
     <comtype>Y</comtype> 
     <camt>-0.65</camt> 
     <currency>GBP</currency> 
     <reascd1>99</reascd1> 
     <reascd2>25</reascd2> 
    </comln>
   </comlines>
 </policy>
 <policy>
  <category>INITIAL</category> 
  <seq>3</seq> 
  <ifaref /> 
  <polnum>4891234</polnum> 
  <schemenum>75999</schemenum> 
  <contype>GP-LINK</contype> 
  <polhold>SMITH-JONES J</polhold> 
   <comlines>
    <comln>
     <comseq>1</comseq> 
     <commdate>24/10/05</commdate> 
     <freq>M</freq> 
     <pamt /> 
     <comtype>Y</comtype> 
     <camt>-14.71</camt> 
     <currency>GBP</currency> 
     <reascd1>99</reascd1> 
     <reascd2>25</reascd2> 
    </comln>
   </comlines>
 </policy>
 <policy>
  <category>INITIAL</category> 
  <seq>4</seq> 
  <ifaref /> 
  <polnum>4931234</polnum> 
  <schemenum>75999</schemenum> 
  <contype>GP-LINK</contype> 
  <polhold>SMITH-JONES J</polhold> 
 <comlines>
 <comln>
  <comseq>1</comseq> 
  <commdate>24/10/05</commdate> 
  <freq>M</freq> 
  <pamt /> 
  <comtype>Y</comtype> 
  <camt>-3.53</camt> 
  <currency>GBP</currency> 
  <reascd1>99</reascd1> 
  <reascd2>25</reascd2> 
  </comln>
  </comlines>
  </policy>
 <policy>
  <category>INITIAL</category> 
  <seq>5</seq> 
  <ifaref /> 
  <polnum>4951234</polnum> 
  <schemenum>75999</schemenum> 
  <contype>GP-LINK</contype> 
  <polhold>SMITH-JONES R</polhold> 
 <comlines>
 <comln>
  <comseq>1</comseq> 
  <commdate>24/10/05</commdate> 
  <freq>M</freq> 
  <pamt /> 
  <comtype>Y</comtype> 
  <camt>-1.28</camt> 
  <currency>GBP</currency> 
  <reascd1>99</reascd1> 
  <reascd2>25</reascd2> 
  </comln>
  </comlines>
  </policy>
 <policy>
  <category>RENEWAL</category> 
  <seq>6</seq> 
  <ifaref /> 
  <polnum>4971234</polnum> 
  <schemenum>70999</schemenum> 
  <contype>GP-LINK</contype> 
  <polhold>SMITH-JONES T</polhold> 
 <comlines>
 <comln>
  <comseq>1</comseq> 
  <commdate>04/11/05</commdate> 
  <freq>M</freq> 
  <pamt>455.75</pamt> 
  <comtype>R</comtype> 
  <camt>11.40</camt> 
  <currency>GBP</currency> 
  <reascd1>01</reascd1> 
  <reascd2>99</reascd2> 
  </comln>
  </comlines>
  </policy>
 <policy>
  <category>RENEWAL</category> 
  <seq>7</seq> 
  <ifaref /> 
  <polnum>4301234</polnum> 
  <schemenum>72999</schemenum> 
  <contype>GP-LINK</contype> 
  <polhold>SMITH-JONES CS</polhold> 
 <comlines>
 <comln>
  <comseq>1</comseq> 
  <commdate>14/11/05</commdate> 
  <freq>M</freq> 
  <pamt>939.59</pamt> 
  <comtype>R</comtype> 
  <camt>9.39</camt> 
  <currency>GBP</currency> 
  <reascd1>01</reascd1> 
  <reascd2>99</reascd2> 
  </comln>
  </comlines>
  </policy>
 <policy>
  <category>RENEWAL</category> 
  <seq>8</seq> 
  <ifaref /> 
  <polnum>4291234</polnum> 
  <schemenum /> 
  <contype>PERPENS</contype> 
  <polhold>SMITH-JONES PJ</polhold> 
 <comlines>
 <comln>
  <comseq>1</comseq> 
  <commdate>20/11/05</commdate> 
  <freq>M</freq> 
  <pamt>500.00</pamt> 
  <comtype>R</comtype> 
  <camt>3.02</camt> 
  <currency>GBP</currency> 
  <reascd1>01</reascd1> 
  <reascd2>99</reascd2> 
  </comln>
  </comlines>
  </policy>
 <policy>
  <category>RENEWAL</category> 
  <seq>9</seq> 
  <ifaref /> 
  <polnum>4821234</polnum> 
  <schemenum /> 
  <contype>XLPLUS</contype> 
  <polhold>SMITH-JONES PR</polhold> 
 <comlines>
 <comln>
  <comseq>1</comseq> 
  <commdate>11/11/05</commdate> 
  <freq>M</freq> 
  <pamt>1862.00</pamt> 
  <comtype>R</comtype> 
  <camt>46.54</camt> 
  <currency>GBP</currency> 
  <reascd1>01</reascd1> 
  <reascd2>99</reascd2> 
  </comln>
  </comlines>
  </policy>
 <totals>
 <total1>
  <totcomm>48.16</totcomm> 
  </total1>
 <total2>
  <commcf /> 
  </total2>
  </totals>
  </doc>
  </xmit>

XSD
Code:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="sample_x0020_XML_x0020_output_x0020_file.xml" xmlns:xs="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema">[/URL]
 <xs:element name="xmit">
  <xs:complexType>
   <xs:sequence>
    <xs:element minOccurs="1" maxOccurs="1" name="dochdr">      
     <xs:complexType>
      <xs:sequence>
       <xs:element minOccurs="0" name="application" type="xs:string" />
       <xs:element minOccurs="0" name="userid" type="xs:string" />
       <xs:element minOccurs="0" name="doctitle" type="xs:string" />
       <xs:element minOccurs="0" name="tranref" type="xs:string" />
       <xs:element minOccurs="0" name="trandate" type="xs:string" />
       <xs:element minOccurs="0" name="trantime" type="xs:string" />
      </xs:sequence>
     </xs:complexType>
    </xs:element>
    <xs:element minOccurs="0" name="senderid" type="xs:string" />
    <xs:element minOccurs="0" name="userid" type="xs:string" />
    <xs:element minOccurs="0" name="icn" type="xs:string" />
    <xs:element minOccurs="0" name="apprefno" type="xs:string" />
    <xs:element minOccurs="0" name="test" type="xs:string" />
    <xs:element minOccurs="0" maxOccurs="unbounded" name="doc">
     <xs:complexType>
      <xs:sequence>               
       <xs:element minOccurs="0" name="brokcode" type="xs:string" />
       <xs:element minOccurs="0" name="inscode" type="xs:string" />
       <xs:element minOccurs="0" maxOccurs="unbounded" name="info">
        <xs:complexType>
         <xs:sequence>
          <xs:element minOccurs="0" name="paydate" type="xs:string" />
	  <xs:element minOccurs="0" name="agencyref" type="xs:string" />
	  <xs:element minOccurs="0" name="ifacode" type="xs:string" />
	 </xs:sequence>
	</xs:complexType>
       </xs:element>
       <xs:element minOccurs="0" maxOccurs="unbounded" name="policy">
	<xs:complexType>
	 <xs:sequence>
	  <xs:element minOccurs="0" name="category" type="xs:string" />
	  <xs:element minOccurs="0" name="seq" type="xs:string" />
	  <xs:element minOccurs="0" name="ifaref" type="xs:string" />
	  <xs:element minOccurs="0" name="polnum" type="xs:string" />
	  <xs:element minOccurs="0" name="schemenum" type="xs:string" />
	  <xs:element minOccurs="0" name="contype" type="xs:string" />
	  <xs:element minOccurs="0" name="polhold" type="xs:string" />
	  <xs:element minOccurs="0" maxOccurs="unbounded" name="comlines">
	   <xs:complexType>
	    <xs:sequence>
	     <xs:element minOccurs="0" maxOccurs="unbounded" name="comln">
	      <xs:complexType>
	       <xs:sequence>
	        <xs:element minOccurs="0" name="comseq" type="xs:string" />
	        <xs:element minOccurs="0" name="commdate" type="xs:string" />
	        <xs:element minOccurs="0" name="freq" type="xs:string" />
	        <xs:element minOccurs="0" name="pamt" type="xs:string" />
	        <xs:element minOccurs="0" name="comtype" type="xs:string" />
	        <xs:element minOccurs="0" name="camt" type="xs:string" />
	        <xs:element minOccurs="0" name="currency" type="xs:string" />
	        <xs:element minOccurs="0" name="reascd1" type="xs:string" />
	        <xs:element minOccurs="0" name="reascd2" type="xs:string" />
	       </xs:sequence>
	      </xs:complexType>
	     </xs:element>
	    </xs:sequence>
	   </xs:complexType>
	  </xs:element>
	 </xs:sequence>
	</xs:complexType>
       </xs:element>
       <xs:element minOccurs="0" maxOccurs="unbounded" name="totals">
	<xs:complexType>
	 <xs:sequence>
	  <xs:element minOccurs="0" maxOccurs="unbounded" name="total1">
	   <xs:complexType>
	    <xs:sequence>
	     <xs:element minOccurs="0" name="totcomm" type="xs:string" />
	    </xs:sequence>
	   </xs:complexType>
	  </xs:element>
	  <xs:element minOccurs="0" maxOccurs="unbounded" name="total2">
	   <xs:complexType>
	    <xs:sequence>
	     <xs:element minOccurs="0" name="commcf" type="xs:string" />
	    </xs:sequence>
	   </xs:complexType>
	  </xs:element>
         </xs:sequence>
        </xs:complexType>
       </xs:element>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element msdata:IsDataSet="true" msdata:Locale="en-GB" name="sample_x0020_XML_x0020_output_x0020_file.xml">
  <xs:complexType>
   <xs:choice maxOccurs="unbounded">
    <xs:element ref="xmit" />
   </xs:choice>
  </xs:complexType>
 </xs:element>
</xs:schema>

As I say the result is a table with only the dochdr elemetn as columns and no data in them.

Many thanks
Nathan
 
I've used XmlToCursor(), CursorToXml() and XmlAdapter object. The first two only work well with simple single-level XML data. Once you get into child, grandchild, etc. data, then it really gets awkward, creating additional records with a stairstep effect, making it hard to programmatically fetch the data.

I have given up trying to get XML into a cursor automatically, as I don't know of one command that can do that well.

I read XML files with the XmlAdapter object which comes with VFP version 9 and perhaps earlier too. I use hand-written code to read and create the XML string, although maybe someone could try to write a generic reader-to-cursor routine. Here is a simple example that reads an XML:
Code:
PRIVATE oXml, iXml
oXml = CREATEOBJECT("XmlAdapter")
oXml.loadXml(IncomingXML)
iXml = oXml.IXMLDOMElement
IF oXml.isLoaded
   cDate = VAL(iXml.getElementsByTagName("EventDate").item(0).text)
   cInfo = VAL(iXml.getElementsByTagName("EventDetail").item(0).text)
ENDIF
RELEASE oXml, iXml
For other comments in the original post, see thread1253-1155506

dbMark
 
Hi dbMark,

Thanks for your help. I wasn't aware of the the getelementsbytagname() option. I will probably use this in my application. If I develop a generic routine then I will certainly let you know about it.

Thanks once again
Nathan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top