Working with XML Documents
Go Up to Working with XML Documents Index
XML (Extensible Markup Language) is a markup language for describing structured data. It is similar to HTML, except that the tags describe the structure of information rather than its display characteristics. XML documents provide a simple, text-based way to store information so that it is easily searched or edited. They are often used as a standard, transportable format for data in Web applications, business-to-business communication, and so on.
XML documents provide a hierarchical view of a body of data. Tags in the XML document describe the role or meaning of each data element, as illustrated in the following document, which describes a collection of stock holdings:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <!DOCTYPE StockHoldings SYSTEM "sth.dtd"> <StockHoldings> <Stock exchange="NASDAQ"> <name>Borland</name> <price>15.375</price> <symbol>BORL</symbol> <shares>100</shares> </Stock> <Stock exchange="NYSE"> <name>Pfizer</name> <price>42.75</price> <symbol>PFE</symbol> <shares type="preferred">25</shares> </Stock> </StockHoldings>
This example illustrates a number of typical elements in an XML document. The first line is a processing instruction called an XML declaration. The XML declaration is optional but you should include it, because it supplies useful information about the document. In this example, the XML declaration says that the document conforms to version 1.0 of the XML specification, that it uses UTF-8 character encoding, and that it relies on an external file for its document type declaration (DTD).
The second line, which begins with the <!DOCType> tag, is a document type declaration (DTD). The DTD is how XML defines the structure of the document. It imposes syntax rules on the elements (tags) contained in the document. The DTD in this example references another file (sth.dtd). In this case, the structure is defined in an external file, rather than in the XML document itself. Other types of files that describe the structure of an XML document include Reduced XML Data (XDR) and XML schemas (XSD).
The remaining lines are organized into a hierarchy with a single root node (the <StockHoldings> tag). Each node in this hierarchy contains either a set of child nodes, or a text value. Some of the tags (the <Stock> and <shares> tags) include attributes, which are Name=Value pairs that provide details on how to interpret the tag.
Although it is possible to work directly with the text in an XML document, typically applications use additional tools for parsing and editing the data. W3C defines a set of standard interfaces for representing a parsed XML document called the Document Object Model (DOM). A number of vendors provide XML parsers that implement the DOM interfaces to let you interpret and edit XML documents more easily.
RAD Studio provides a number of additional tools for working with XML documents. These tools use a DOM parser that is provided by another vendor, and make it even easier to work with XML documents.They include
- VCL components and interfaces for Working with XML Components.
- An XML Data Binding wizard for generating classes to represent a particular XML document; see Abstracting XML Documents with the Data Binding Wizard.
- Tools and components for converting between XML documents and data packets, which let you integrate XML documents into database applications.