Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
XMLTOCURSOR() and bad characters
Message
De
15/03/2020 16:33:51
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
XML, XSD
Divers
Thread ID:
01673619
Message ID:
01673631
Vues:
62
J'aime (1)
Dmitry,

You can create a CDATA section as part of a element text by enclosing it between <![CDATA[ and ]]>. Inside the CDATA section you can use (almost) any character, including the ones that are used in the XML codification (<, > and &).

If you use this approach, the only issue that remains is that you must be sure that the CDATA ending sequence (]]>) is properly escaped - that was the purpose of the STRTRAN() in the code I suggested.

Another approach would be to let the MSXML parser set the contents of each element. This could be simpler to read... You prepare a template with the required elements, and then fill them with a simple set.

Here it goes (using most of the code I suggested previously):
CREATE CURSOR CUR_TEMP (NAME C(20), OV M, NV M)

LOCAL UserProvidedName AS String
LOCAL UserProvidedOV AS String
LOCAL UserProvidedNV AS String

LOCAL cAuditDescr AS String

m.UserProvidedName = "Benny & Sons, LLC"
m.UserProvidedOV = "This tests ]]> the end of CDATA section."
m.UserProvidedNV = "Some long description with <, >, & and so forth..."

LOCAL XMLTemplate AS String

TEXT TO m.XMLTemplate NOSHOW
<?xml version="1.0"?>
<VFPData>
	<Fld>
		<Name />
		<OV />
		<NV />
	</Fld>
</VFPData>
ENDTEXT

LOCAL XML AS MSXML2.DOMDocument60

m.XML = CREATEOBJECT("MSXML2.DOMDocument.6.0")

m.XML.LoadXML(m.XMLTemplate)

* now, just set the values of each element
m.XML.Selectnodes("//Fld/Name").item(0).text = m.UserProvidedName
m.XML.Selectnodes("//Fld/OV").item(0).text = m.UserProvidedOV
m.XML.Selectnodes("//Fld/NV").item(0).text = m.UserProvidedNV

MESSAGEBOX(m.XML.XML)

XMLTOCURSOR(m.XML.XML, "CUR_TEMP", 8192)

BROWSE
>Hi Antonio,
>
>I am trying to apply your suggested code to my case. In my case I get the substring (between, for example, NV and NV (with brackets). And then I have to insert this substring back into the long string, using your CDATA tags. Here is my example:
>
>cOvString = STREXTRACT( C_WOAUDIT.AUDIT_DESC, "<Fld><Name>WODESCR</Name><OV>","</OV>")
>*-- So the string cOvString has bad characters.  
>*-- Now I need to put this back into the field AUDIT_DESC and add the CDATA tags.  My attempt:
>REPLACE AUDIT_DESC with 
>STRTRAN( AUDIT_DESC, "<Fld><Name>WODESCR</Name><OV>" + cOvString + "</OV>", 
>"<Fld><Name>WODESCR</Name><OV>![CDATA[" + cOvString + "![CDATA]</OV>" ) IN C_WOAUDIT
>
>
>Did I add the exclamation sign and the CDATA correctly? I don't know if you had the exclamation sign as part of the sample code or it has to be in the string.
>
>TIA
>
>
>
>>Dmitry, you may encapsulate the user provided data inside CDATA sections, and then build the XML elements using them.
>>
>>
>>CREATE CURSOR CUR_TEMP (NAME C(20), OV M, NV M)
>>
>>LOCAL UserProvidedName AS String
>>LOCAL UserProvidedOV AS String
>>LOCAL UserProvidedNV AS String
>>
>>LOCAL cAuditDescr AS String
>>
>>m.UserProvidedName = "Benny & Sons, LLC"
>>m.UserProvidedOV = "This tests ]]> the end of CDATA section."
>>m.UserProvidedNV = "Some long description with <, >, & and so forth..."
>>
>>TEXT TO m.cAuditDescr TEXTMERGE NOSHOW
>><?xml version="1.0"?>
>><VFPDATA >
>>	<row>
>>		<name><![CDATA[<<STRTRAN(m.UserProvidedName, "]]>", "]]>]]&" + "gt;<![CDATA[")>>]]></name>
>>		<ov><![CDATA[<<STRTRAN(m.UserProvidedOV, "]]>", "]]>]]&" + "gt;<![CDATA[")>>]]></ov>
>>		<nv><![CDATA[<<STRTRAN(m.UserProvidedNV, "]]>", "]]>]]&" + "gt;<![CDATA[")>>]]></nv>
>>	</row>
>></VFPDATA>
>>ENDTEXT
>>
>>MESSAGEBOX(m.cAuditDescr)
>>
>>XMLTOCURSOR(m.cAuditDescr, "Cur_Temp", 8192)
>>
>>BROWSE
>>
>>
>>
>>>Antonio,
>>>
>>>The XML is based on a content of a memo field (editbox) on one of the forms.
>>>What I am thinking to do is, before converting the memo into the string and into the XML, search for any of these "bad" characters and convert them to "good" characters. For example, convert
<
to LT,
>
to GT and
&
to Ampersand.
>>>I don't know how often users actually use these characters. I came upon them when testing and using my sample DB where I use all kind of weird characters.
>>>Thanks
>>>
>>>>Dmitry,
>>>>
>>>>Since you're allowing the user to edit or prepare the XML document, there is not much you can do besides error trapping. Using current parsers like MSXML you cannot turn a not well-formed XML document into a well-formed XML document. You would have to write a parser for the effect.
>>>>
>>>>Does the user has to prepare the data for your system in XML?
>>>>
>>>>>Hi,
>>>>>
>>>>>I am converting a string into a cursor. The string has values entered by the user (so, I have no control). Here is the simple example:
>>>>>
>>>>>CREATE CURSOR CUR_TEMP (NAME C(20), OV M, NV M)
>>>>>XMLTOCURSOR( cAuditDescr, 'CUR_TEMP', 8192 )
>>>>>
>>>>>
>>>>>What happens is the string cAuditDescr could have characters such as a Less-Than (
<
, Ampersand
&
, Greater-Than (
>
). Any of these characters cause a run-time error.
>>>>>
>>>>>How do you suggest I change the code above to allow the values to be in the cursor ("CUR_TEMP") without a run-time error?
>>>>>
>>>>>TIA
----------------------------------
António Tavares Lopes
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform