Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Create a cursor from an XML file
Message
From
02/01/2017 09:31:24
 
 
To
02/01/2017 07:19:06
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
01646245
Message ID:
01646256
Views:
49
>>Hi.. Happy New Year!
>>
>>Is it possible (and if yes, how?) to create a cursor (of course, at run time) from a structure defined in an XML file included in the application EXE? What would the XML look like?
>>TIA
>
>Well, I should have read your requirement a bit more carefully... It's not about data, it's about structure...
>
>The answer would be: "the XML would look like an XML Schema". Unfortunately, as far as I could understand, XMLTOCURSOR() interprets only the first element definition in a schema, so it seems that there is no way to hold different structure definitions inside a single schema that can be independently referenced.
>
>If you really prefer to have all definitions in a single file, as you stated (but Thomas presented a few arguments against it you should at least consider), an alternative approach would be to create the XML with an inline schema, and grab the definition from the overall schema pool.
>
>Something like this:
>a) start by create the cursors schema and store it in cursors.xsd
>b) the function CreateCursorFromXSD creates an XML string with an empty cursor top element, and an inline schema fetched from the cursors.xsd
>c) and then invokes XMLTOCURSOR() to create an empty cursor
>
>
>LOCAL CursorDefinitions AS String
>
>TEXT TO m.CursorDefinitions NOSHOW FLAGS 1
><?xml version="1.0" encoding="windows-1252"?>
><xsd:schema id="VFPSchema"
>  targetNamespace="http://microsoft.com"
>  xmlns="http://microsoft.com"
>  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>  xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" elementFormDefault="qualified">
>  <xsd:element name="VFPData">
>    <xsd:complexType>
>      <xsd:choice maxOccurs="unbounded">
>		  <!-- the cursor definitions -->
>		  <!-- PERSONS -->
>		  <xsd:element name="person">
>			  <xsd:complexType>
>			    <xsd:sequence>
>			      <xsd:element name="name">
>			        <xsd:simpleType>
>			          <xsd:restriction base="xsd:string">
>			            <xsd:maxLength value="50"/>
>			          </xsd:restriction>
>			        </xsd:simpleType>
>			      </xsd:element>
>			      <xsd:element name="gender">
>			        <xsd:simpleType>
>			          <xsd:restriction base="xsd:string">
>			            <xsd:maxLength value="1"/>
>			          </xsd:restriction>
>			        </xsd:simpleType>
>			      </xsd:element>
>			      <xsd:element name="born" type="xsd:date"/>
>			    </xsd:sequence>
>			  </xsd:complexType>
>		  </xsd:element>
>		  <!-- CARS -->
>		  <xsd:element name="car">
>			  <xsd:complexType>
>			    <xsd:sequence>
>			      <xsd:element name="brand">
>			        <xsd:simpleType>
>			          <xsd:restriction base="xsd:string">
>			            <xsd:maxLength value="50"/>
>			          </xsd:restriction>
>			        </xsd:simpleType>
>			      </xsd:element>
>			      <xsd:element name="model">
>			        <xsd:simpleType>
>			          <xsd:restriction base="xsd:string">
>			            <xsd:maxLength value="50"/>
>			          </xsd:restriction>
>			        </xsd:simpleType>
>			      </xsd:element>
>			      <xsd:element name="year" type="xsd:int"/>
>			    </xsd:sequence>
>			  </xsd:complexType>
>		  </xsd:element>		  
>      </xsd:choice>
>    </xsd:complexType>
>  </xsd:element>
></xsd:schema>
>ENDTEXT
>
>STRTOFILE(m.CursorDefinitions, "cursors.xsd")
>
>CreateCursorFromXSD("persons", "person")
>CreateCursorFromXSD("cars", "car")
>
>SELECT Persons
>BROWSE NOWAIT
>
>SELECT Cars
>BROWSE NOWAIT
>MOVE WINDOW Cars BY 5, 10
>
>FUNCTION CreateCursorFromXSD (CursorName AS String, CursorType AS String) AS Boolean
>
>	LOCAL CursorCreator AS String
>	LOCAL XSD AS MSXML2.DOMDocument60
>	LOCAL CursorDefinition AS MSXML2.IXMLDOMNodeList
>
>	m.XSD = CREATEOBJECT("MSXML2.DOMDocument.6.0")
>	m.XSD.Async = .F.
>	m.XSD.Load("cursors.xsd")
>	m.XSD.Setproperty("SelectionNamespaces", "xmlns:xsd='http://www.w3.org/2001/XMLSchema'")
>	m.CursorDefinition = m.XSD.Selectnodes("//xsd:element[@name='" + m.CursorType + "']")
>	
>	SET TEXTMERGE DELIMITERS TO "««", "»»"
>	
>	USE IN SELECT(m.CursorName)
>
>	TEXT TO m.CursorCreator TEXTMERGE NOSHOW FLAGS 1 PRETEXT 3
>		<?xml version="1.0" encoding="windows-1252"?>
>		<VFPData xmlns:xsd="http://www.w3.org/2001/XMLSchema"  xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
>			<xsd:schema id="VFPData">
>				<xsd:element name="VFPData" msdata:IsDataSet="true">
>					<xsd:complexType>
>						<xsd:choice maxOccurs="unbounded">
>							««m.CursorDefinition.item(0).xml»»
>						</xsd:choice>
>					</xsd:complexType>
>				</xsd:element>
>			</xsd:schema>
>			<««m.CursorType»» />
>		</VFPData>
>	ENDTEXT
>	
>	SET TEXTMERGE DELIMITERS
>
>	XMLTOCURSOR(m.CursorCreator, m.CursorName)
>
>ENDFUNC
>
Hi Antonio el al,

Thank you very much and everyone else for your input and the code. Excellent learning for me.

But as far as my question, last night (after I already turned off my computer), I realized that I can do what I need in a very simple way. You see I know before hand what structure this or that cursor should have. When hard coding I use the syntax:
create cursor MyCursor1 (field1 c(10), field2 n(12,2), field3 i)
and so on. But I wanted to have the structure of these cursors outside of the code so that I can change it, if necessary, without changing the program. So the approach I though is as follows:
*** text file MyCursors.txt
<Cursor1Name>field1 c(10), field2 n(12,2), field3 i</Cursor1Name>
<Cursor2Name>field1 c(40), field2 n(1,2), field3 i</Cursor21Name>
So, at run-time, I can read this .txt (or other extension) file using filetostr(). And, then using STREXTRACT() get the structure of any cursor. It will be a very small Function. Then use the extracted line of the Structure in the CREATE CURSOR command.

Again, thank you for your help!
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform