Functions and Directives

Function Xml2Var() Foundation

Converts an XML-encoded character string back to the original value.

Xml2Var( <cXmlFragment> ) --> xValue
<cXmlFragment> is an XML-encoded character string representing any supported value.

The function Xml2Var() returns the XML-encoded value in a native Xbase++ data type.


Xml2Var() is the inverse function of Var2Xml(). An XML-encoded character string is converted back to the original value with Xml2Var(). The XML string may also be created manually or modified after being created by Var2Xml(), but it must follow the syntax used by Var2Xml().

Restoring character strings: Characters specified using numeric character references in decimal or hexadecimal notation (eg. & or ä) are not supported, and will not be converted back to their original character value. Xml2Var() will also remove all leading white-space, tab and carriage return/linefeed characters. All other characters, including trailing white-space characters, will be kept and returned in the converted string.

Restoring Objects: The classname attribute of the XML start tag specifies the class name of the object. If the class specified in the <object> tag's classname attribute isn't available at runtime, Xml2Var() will instead create a DataObject() with the named instance variables.

The Xml2Var() function will not automatically call the :notifyLoaded() method of the class to handle non-persistent object data, while the Bin2Var() function calls this method automatically.

Sample of XML fragment, value of variable cXML in example below
<object classname="TestObject"> 
<undefined name="undefined"/> 
<codeblock name="codeblock">{|x| x + 1}</codeblock> 
<logical name="logical">true</logical> 
<character name="character">String with < & > ' "</character> 
<numeric name="number">123.456</numeric> 
<array name="array"> 
<object classname="DataObject" name="object"> 
  <character name="test">Test of Data Object in Test Object</character> 
  <array name="array"> 
      <character>Some Text</character> 
      <codeblock>{|x| x ^ 2}</codeblock> 
<date name="date_var">20160123</date> 
Convert XML-encoded strings to original values

// The example shows how to restore values from XML-encoded strings 
// using Xml2Var(). 

   LOCAL cXml, oCopy 
   LOCAL oData := TestObject():new() 

   oData:array        := {123, 'Text', Date()} 
   oData:codeblock    := {|x| x + 1} 
   oData:character    := "String with < & > ' " + Chr(34) 
   oData:date_var     := CtoD('01/23/2016') 
   oData:logical      := .t. 
   oData:number       := 123.456 
   oData:undefined    := NIL 

   oData:object       := DataObject():new() 
   oData:object:test  := 'Test of Data Object in Test Object' 
   oData:object:array := {{1, 2, 3}, {.t., "Some Text", Date()}, {{|x| x ^ 2}}} 

   cXml := Var2Xml(oData)    // cXml now describes the TestObject oData 
   ? cXml 

   oCopy := Xml2Var(cXml)    
   ? oCopy:character 
   ? oCopy:object:test 

CLASS TestObject 
   VAR array 
   VAR codeblock 
   VAR character 
   VAR date_var 
   VAR logical 
   VAR number 
   VAR object 
   VAR undefined 

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.