*property_to_xml Function


dim resultXML as C = *PROPERTY_TO_XML(prop as p,topTag as c[,flags as c])



The dot variable to convert to XML.


The name of the top-level XML tag.


Optional flags.



Returns an XML representation of the dot variable.


Save property to XML.

DIM test as P
test.namefirst = "John"
test.namelast = "Smith"
test.otherxml =<<%xml%
<body>Don't forget Wednesday's Q&mp;A session!</body>

DIM output  as C
output = *property_to_xml(test,"test")
? output
= <test
namefirst = "John"
namelast = "Smith"
otherxml = "&lt;note&gt;
&lt;body&gt;Don't forget Wednesday's Q&amp;mp;A session!&lt;/body&gt;
" />

Understanding __A5_elementContent and __A5_Xml_Manifest

Some meta information is included when XML content is converted to a dot variable. This information is required in order to provide round-trip support (convert XML to a property array and then back to original XML format.) Consider the following very simple snippet of XML.

xml = <<%txt%
<name city-name="boston">
    Fred Smith

If you view this XML using the XML viewer function (showXML()), you will see this:


You will notice that the XML has an attribute called 'city-name'. This is not a valid Xbasic variable name, so the attribute has to be renamed (to 'city_name'). In order that the *property_to_xml() function can get back to the same XML that was originally parsed, a list of all of the attribute names that were changed is kept in the special __A5_Xml_Manifest property.


Also, you will notice that in the XML snippet, the 'name' element has a value and also it has attributes. The attribute values are shown as properties and the element value is shown using the special property name '__A5_elementContent'.

Here is how you can parse the above XML into an Xbasic dot variable:

delete p
dim p as p

'set the optional 3rd flag to .t. to use the special properties

'convert the dot variable into a script so we can 'see' what's in the variable
= DIM name as P
DIM name.city_name as C = "boston"
DIM name.__A5_elementContent as C = <<%str%

Fred Smith
DIM __A5_Xml_Manifest as C = <<%str%

'now, go back to XML
= <name city-name = "boston" >
    Fred Smith

Notice how this is a perfect 'round-trip'! The generated XML is the same as the initial XML that was parsed. Now, try the above exercise without using the new optional flag on the *property_to_xml() function

delete p
dim p as p


'convert the dot variable into a script so we can 'see' what's in the variable

= DIM name as P
DIM name.city_name as C = "boston"

Notice how the Xbasic dot variable only has the value of the attribute. It does NOT have the value of the element!

See Also