C *PROPERTY_FROM_XML(xml as c,prop as p[, reversible as l])
The XML to parse into a dot variable.
A dot variable that will be populated with the parsed XML.
Default = .f. If .t., mapping information for the XML format will be included in the prop variable.
Read property from XML, if reversible flag is set, the XML we generate will match (adds levels to handle scaling tags - i.e. adding runs of tags and mapping names).
*property_from_xml() populates an Xbasic Dot Variable with XML Attribute/Element Values. Xbasic has always had very strong XML parsing features (see Parsing XML Documents with Xbasic), however the new *property_from_xml() function provides a new way in working with XML. This function populates an Xbasic dot variable with the data from the XML document. Once you have he Xbasic variable, it is very easy to extract a specific element or attribute value from the XML document.
dim xml as c = http_get_page2("http://feeds.gawker.com/lifehacker/full") dim content as p *property_from_xml(xml,content) dim xmlvalue as c xmlvalue = content.rss.channel.item.title 'to get all of the titles, use the .dump_properties() method dim allTitles as c allTitles = content.rss.channel.item.dump_properties("title")
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 </name> %txt%
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 *property_from_xml(xml,p,.t.) 'convert the dot variable into a script so we can 'see' what's in the variable ?*variable_to_script(p) = DIM name as P DIM name.city_name as C = "boston" DIM name.__A5_elementContent as C = <<%str% Fred Smith %str% DIM __A5_Xml_Manifest as C = <<%str% @Mapping: city_name=city-name%str% 'now, go back to XML ?*property_to_xml(p,"") = <name city-name = "boston" > Fred Smith </name>
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 *property_from_xml(xml,p,.f.) 'convert the dot variable into a script so we can 'see' what's in the variable ?*variable_to_script(p) = 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!
Working with XML Documents
The *property_from_xml() function and XML document viewer make it easy to work with XML documents. Watch the video below to learn how to use these features when working with XML documents using Xbasic.