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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML - How to use XSD

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
XML Newbie. Help. I need to create an XML file from some tables in VFP9. I have created a very simple XML file using CursorToXml. The people I'm sending the XML file to are requiring me to conform to their XSD data format. I have no idea where to start. Are there Vfp commands or functions that read XSD files and use the XSD file to create the XML file, or do I have to manually go throught the XSD file to figure out exactly what they want and create my cursor to match the XSD? All the articles I have seen and my basic testing actually create an XSD file. I guess I'm confused as to how I should use their XSD file. See, I told you I was an XML newbie.

Auguy
 
Check out CursorToXML(). You can specify an XSD file to use in the conversion.

Tamar
 
Thanks Tamar,

If I use the following code:

CURSORTOXML("myCursor", "TestXml.xml", 1, 512, 0, "TestXml.xsd")

It seems to overwrite the XSD. Am I missing a parameter setting?

Auguy
 
CURSORTOXML("myCursor", "TestXml.xml", 1, 512, 0, "TestXml.xsd")

It seems to overwrite the XSD. Am I missing a parameter setting?

You are not missing anything. CURSORTOXML() produces XML in its own format and, therefor, proces its own schema.

Your problem is the reverse - you need to produce XML that conforms to a specific schema. You will not be able to do this using CURSORTOXML(). You may be able to do this using the XMLadapter, but I do not know for sure since my experience with this class is somewhat limited. I do know that it expses a SOM property which Contains an object reference to the XML Schema Object Model (SOM) document (ISchema).

You will probably have to write some code to produce the XLM document using the XML DOM ( I think it is up to version 6 right now ) and analyzing the schema.

You may find some help at
Marcia G. Akins
 
OK, I'm still confused about trying to use an XSD file to help create an XML file. Is there any method or tools that use the XSD file to create the XML file assuming you have the correct field names in your cursor? Everything I have read says you can validate the XML against the XSD after the XML file is created to make sure it conforms, but shouldn't there be some way of using the XSD to assist in properly creating the XML in the first place. If you can't do something like this, then I guess you have to create the XML file manually which seems like way to much work. Can anyone shed any more light on this for me? Am I missing some setting the xmladapter? HELP!

Auguy
 
Hi Auguy,

I am having the same exact problem. When i create an XML from CURSORTOXML it just overwrites my schema that i specified with its generic one. Have you found a solution? I will be sure to post if i find a solution, could you please keep me posted on your quest too.

I am currently trying to learn the DOM that has been talked about previously. But i have no idea about XML, so it is proving difficult.

Thanks
Andrew
 
Sorry Andrew. I never did find anything that I thought would work. Maybe I just don't have enough knowledge to understand all of the information I did find. I eventually just wrote code to make the XML file.

Auguy
 
I too have just writen the code to create the XML file. What a job! I had no idea what i was doing. But it is all done now. If anyone else is wondering how to create the XML file manually, here is a snippet of my code. A lot of this code was hi-jacked from Simon Arnold's article:

I found it very useful. I cheated and asked the company who provided the Schema for an example XML file. It was much easier to replicate the file than to follow the Schema.

Here is the code. I had to add an attribute node to the element node. That was not from Simon's example.

** Start the DOM
oXML = CreateObject("MSXML.DOMDocument")
** Add Encoding line
oXML.appendChild(oXML.createNode("PROCESSINGINSTRUCTION","xml",""))
** Create Root Node "fields"
oRoot = oXML.appendChild(oXML.createNode("ELEMENT","fields",""))

** Create Element "field"
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
** Create Attribute "name"
newatt=oXml.createAttribute("name")
** Create Attribute Value "b12c96nfAccount_name"
newatt.value="b12c96nfAccount_name"
** Attach Attribute to Element "field"
oChild.setAttributeNode(newatt)
** Create Element "value"
oChild.appendChild(oXML.createElement("value"))
** Create Text Node (data) attached to Element "value"
oChild.lastChild.text = ALLTRIM(LEFT(pvaname,26))

** Account No
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfAccount_no"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = pvano
** Address Line 1
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfAddress_line_1"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = ALLTRIM(LEFT(pvaddress,30))
** BSB
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfBSB"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = pvbsb
** Company Name
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfCompany_name"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = ALLTRIM(LEFT(pvshopname,30))
** Date Signed
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfDate_signed_1"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = val(LEFT(DTOC(DATE()),2)+SUBSTR(DTOC(DATE()),4,2)+RIGHT(DTOC(DATE()),4))
** DOB
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfDOB"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = val(LEFT(DTOC(pvdob),2)+SUBSTR(DTOC(pvdob),4,2)+RIGHT(DTOC(pvdob),4))
** Email
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfEmail"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = ALLTRIM(LEFT(pvemail,30))
** First Name
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfFirst_Name"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = ALLTRIM(LEFT(pdffirst,30))
** Bank
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfInstitution"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = ALLTRIM(LEFT(pvbn,26))
** Branch
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfInstitution_suburb"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = ALLTRIM(LEFT(pvb,26))
** Mobile
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfMobile_phone"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = ALLTRIM(pvmobile)
** Phone
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfPhone"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = ALLTRIM(pvphone)
** Post_code
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfPost_code"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = pvpostcode
** Principal
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfPrincipal"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = ALLTRIM(LEFT(pvshopname,24))
** Reference ezypay no
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfReference_id"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = pdfcustrefid
** State
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfState"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = ALLTRIM(LEFT(pvstate,3))
** Suburb
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfSuburb"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = ALLTRIM(LEFT(pdfsuburb,26))
** Surname
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfSurname"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = ALLTRIM(LEFT(pdflast,30))

oXML.Save("c:\data.xml")
 
Oops,

I forgot to mention all my data was held in variables. My XML file only has 1 record at a time. If you wanted to build an XML with several records, perhaps from a table, it would be simple to add a scan into the code. Something like this:

use customer
oXML = CreateObject("MSXML.DOMDocument")
oXML.appendChild(oXML.createNode("PROCESSINGINSTRUCTION","xml",""))
oRoot = oXML.appendChild(oXML.createNode("ELEMENT","fields",""))
Scan
** Optional Query
if surname = "SMITH"
oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfAccount_name"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = ALLTRIM(LEFT(pvaname,26))

oChild = oRoot.appendChild(oXML.createNode("ELEMENT","field",""))
newatt=oXml.createAttribute("name")
newatt.value="b12c96nfAccount_no"
oChild.setAttributeNode(newatt)
oChild.appendChild(oXML.createElement("value"))
oChild.lastChild.text = pvano
endif
endscan
oXML.Save("c:\data.xml")

Alternatively you may use a Do While loop instead of a scan
DO WHILE NOT EOF()
SKIP
ENDDO
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top