Extract and Display Data
The first example is fairly simple. My name and age are stored in the text file "data1.txt." Listing 1
shows how to display my name and age using the tag.
The output will display:
In Listing 1, note the attributes within the SPAN tags. DATASRC specifies the data source to usethe same as the ID of the object initialized, in this case, "data1." The DATAFLD attribute specifies which field is to be displayed. Since the "data1.txt" file has two fields ("name" and "age"), specifying the DATAFLD as "name" will display the name.
You can use this same method to extract data from a text file into any HTML element. However, there is a flaw. If your data file contains more than one entry, you wont be able to extract all the values directly.
In these cases, use the TABLE tag.
The next example will illustrate how you can use the TABLE tag to solve this problem.
Display in a Tabular Form
Listing 2 demonstrates how to extract this data and display it on the Web page in a tabular form. The text file in this example stores the name, age, and sex of three people:
The code in Listing 2
contains three data fields (DATAFLD) in three different tags (columns) only once. The Web page automatically displays all three sets of values (three rows). You can add as much content as you want to the text file without making any modifications to the HTML code that extracts these values.
The output looks like this: