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!

xml to table as character problem

Status
Not open for further replies.

gfrlaser1

Technical User
Jan 31, 2013
28
US
I am importing from an xml using simple statements such as:

Xmltocursor("&Mxm", "cxm", 512)

however, on field in the xml is needed as character I my new table. The problem appears to be that there is a '-' in the xml field. As an example in xml it is 45987-8.
on import to cursor, the field imports as numeric and the '-8' is truncated or dropped off. Any idea how to force import so that this particular fields comes in as character?

Thanks for any input

Will
 
If your XML does not contain schema info VFP has to guess. You can avoid that by specifying a cursor to use as the target of XML conversion.

Simple example:
1. I create a VFP cursor with an int field and convert to XML without schema info
2. converting back to cursor you get the int field back.
3. Creating a cursor with a char field and specifying it as the target of conversion the int is put into the char field. Voila.

Code:
CLEAR
Create Cursor crsTest1 (aString Char(10))
Insert Into crsTest1 Values (PADL(42,9))
? crsTest1.aString, VARTYPE(crsTest1.aString)

CURSORTOXML("crsTest1","lcXML") && no use of further parameters means creating pretty bad XML, ie without schema
* reconvert without target cursor predefined
XMLTOCURSOR(lcXML,"crsTest2")
? crsTest2.aString, VARTYPE(crsTest2.aString)

* reconvert with a target cursor
CREATE CURSOR crsTest3 (aString Char(10)) && we define what we want
* reconvert specifying to use that target cursor
XMLTOCURSOR(lcXML,"crsTest3",8192) && 8192 means crsXML should be taken as the name of an existing cursor and appended to, not be (re)created!
? crsTest3.aString, VARTYPE(crsTest3.aString)
?
? lcXML

What you'll see as output here is the original char(10) field with a right aligned 42 is turned to a numeric field in the first reconversion, but is turned back as char field by specifiyng the target cursor. The only thing not transferred is the right alignment. The XML shows why, the XML element for the aString field is trimmed, so there is no way to turn this back exactly as it was.

There are further options to adjust properly to get spaces out as they were, for example. Take a close look into the description of the last nFlags parameter and what the values there mean and do. Experiment, if you don't get it from reading alone.

But in short: If you specify a target cursor, you have control about the field types.

And what I can't reproduce is your case of a "42334-8" turning into a number 42334 without the -8. If I put something like that into aString even the unguided simple reconversion turns that to a char field again.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top