Partager via


XML Data in Visual FoxPro

You can work with XML data in Visual FoxPro by importing or exporting XML documents using the Visual FoxPro functions or XMLAdapter objects. The following sections introduce valid, or "well-formed", XML documents and XML schemas, which help explain requirements for working with XML data in Visual FoxPro:

  • Well-Formed XML Docments

  • Support for XML Schemas

Well-Formed XML Docments

XML documents are valid when they are "well-formed". Visual FoxPro produces XML documents that are well-formed. A well-formed document conforms to the basic rules of XML:

  • Each XML document must have a unique root element, which is an element that encompasses the entire document.

  • All start and end tags match. XML tags are case-sensitive.

  • For each start tag, a corresponding end tag exists. A special shorthand tag can denote empty elements.

  • Elements do not overlap. In other words, start and end tags must be properly nested within other elements.

  • Certain reserved characters are part of the XML syntax and are not interpreted as they are if used in the data portion of an element. The following table lists special character sequences, or entities, used to substitute for these reserved characters.

    Character

    Data type

    Entity encoding

    &   (ampersand) <   (left angle bracket) >   (right angle bracket) "   (quotation mark) '   (apostrophe)

    String String String String String

    Replace with &amp; Replace with &lt; Replace with &gt; Replace with &quot; Replace with &apos;

    For other data types, the following table lists rules for entity encoding.

    Data type

    Character and entity encoding

    Date

    Must follow the ISO 8601 format

    Numbers

    Punctuation must use U.S. English rules. For example, you must use a period as a decimal separator. Numbers can include exponents.

    Boolean

    False = 0, True = 1. (SQL XML returns 0 and1)

    BLOB

    Use MIME Base64 encoding

The following example shows a well-formed XML document:

<?xml version="1.0"?>
<Data>
<ORDER>
 <CUSTOMER>Mary Baker</CUSTOMER>
  <ITEM>Coho Winery&apos;s Chablis</ITEM>
 <PRICE>$10.00</PRICE>
 <QUANTITY>1 Bottle</QUANTITY>
 </ORDER>
</Data>

Tip

You can use white space throughout the document to enhance readability.

In the example, the code illustrates certain parts of the XML document:

  • <?xml version="1.0"?>

    Declares the XML document and provides the version number. This declaration is optional but recommended in any XML document.

  • <ORDER>

    Specifies the root element that encompasses the entire document.

  • <CUSTOMER> ... </CUSTOMER>

    Specifies a start and end tag set, which describes an element of data, in this case, the customer's name.

    Note

    Each tag set has both start and end tags, is case sensitive, and is properly nested. When the data is imported by the receiving application, the entity &apos is transformed to an apostrophe ('). The apostrophe has a special purpose in an XML document and might be misinterpreted if used directly in text. The converted data is displayed as Coho Winery's Chablis.

Support for XML Schemas

XML schemas can help define the rules and structure for XML documents. Visual FoxPro can interpret XML files with or without schemas. However, Visual FoxPro supports XML Schema Definition (XSD), which is a basic infrastructure for describing the type and structure of XML documents. An XML schema definition serves several purposes:

  • Describes the structure of data in a common format that customers, other Web browsers, and any number of XML-enabled software programs can recognize.

  • Defines the rules of an XML data document, including element names and data types, which elements can appear in combination, and which attributes are available for each element.

  • Provide a model for an XML data document by defining the arrangement of tags and text within all documents referencing the schema.

By using an XML schema, you can ensure that any XML document used to import or export data contains specific data and conforms to a defined structure. When you specify a schema when exporting XML from Visual FoxPro, the exported XML documents are considered valid XML. This means that in addition to being well-formed, the documents conform to a defined schema. You can also provide a schema to other businesses and applications so that they can structure XML data they provide to you as well as provide their schema to you.

In particular, Visual FoxPro supports the W3C XSD schema format. The XSD schema format is based on the W3C Recommendation of the XSD Schema specification. For more information, see https://www.w3.org/TR/2001/REC-xmlschema-0-20010502/ and related documents.

See Also

Concepts

Converting Between XML and Visual FoxPro Data

XML Functionality Using XMLAdapters

Reference

XML Functions

Other Resources

Data and Field Types