Including XML documents

In Listings 8.1 and 8.2 you saw that you could embed the XML document containing data in your LZX application. While this is fine when the data is small, it is not always the best approach. Some data are big and complex and might obscure code if they are put together. Some data are dynamic and come from other servers and not known at compile time.

The dataset tag has the src attribute that lets you include XML data from an external source. There are two uses of this attribute, depending upon whether the data is static or dynamic.

Including static data

If the data is static, the only requirement is to store it in a file. You then store the file in a resource that the OpenLaszlo compiler can find at compile time. For example, the code in Listing 8.3 has the same effect as the application in Listing 8.2 that embeds the data.

Listing 8.3. Including static data
<canvas height="200" width="500">
    <dataset name="myData" src="library.xml"/>
    <view layout="axis:y">
        <text width="200"
                datapath="myData:/library/book[1]/title/text()"/>
        <text width="200"
                datapath="myData:/library/book[2]/title/text()"/>
        <text width="200"
                datapath="myData:/library/book[3]/title/text()"/>
    </view>
</canvas>

Including dynamic data

If the data is dynamic and comes from a Web server, you don’t include it at compile time. Instead, you tell the application to download it at runtime. Listing 8.4 shows how to include dynamic data.

Listing 8.4. Including dynamic data
<canvas height="200" width="500">
    <dataset name="myData" request="true"
            src="http://localhost:8080/lps-4.0.x/test/library.xml"/>
    <view layout="axis:y">
        <text width="200"
                datapath="myData:/library/book[1]/title/text()"/>
        <text width="200"
                datapath="myData:/library/book[2]/title/text()"/>
        <text width="200"
                datapath="myData:/library/book[3]/title/text()"/>
    </view>
</canvas>

The request=“true” part in the dataset tag tells the generated output to download the data as soon as the application loads.

Note

The autorequest attribute does the same thing as request, but autorequest is deprecated.


In Listing 8.4, the src attribute is assigned an absolute URL. You can also assign it a relative URL, as shown in Listing 8.5.

Listing 8.5. Using a relative URL
<canvas height="200" width="500">
    <dataset name="myData" request="true" type="http"
            src="http:library.xml"/>
    <view layout="axis:y">
        <text width="200"
                datapath="myData:/library/book[1]/title/text()"/>
        <text width="200"
                datapath="myData:/library/book[2]/title/text()"/>
        <text width="200"
                datapath="myData:/library/book[3]/title/text()"/>
    </view>
</canvas>

Note that the value of the src attribute starts with http:. In this case, the library.xml document is in the same directory as the compiled LZX file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset