Getting Started with the XML Data Source Object
When Internet Explorer first rolled out support for XML in the early 2000s, Microsoft introduced the XML Data Source Object, or XML‑DSO, as a lightweight ActiveX control. Its purpose is to let a web page read, parse, and bind XML data without the need for external libraries or server‑side processing. Because the control lives inside the browser, it can be accessed through HTML markup or scripted via JavaScript, making it a convenient tool for small to medium‑sized XML projects.
To use the XML‑DSO you only need a single <OBJECT> element in your page. The element carries a CLASSID that tells the browser to instantiate the control. The CLSID for XML‑DSO is clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39. No additional parameters are required, so the element can be minimal:
The width and height are set to zero because the object itself does not render anything on the page. It simply exists in memory so that the rest of the document can reference it for data binding. The id attribute gives the control a unique name so you can address it later from JavaScript or from other HTML tags that support data binding.
Once the object is in place, two core features become available. First, the object exposes a XMLDocument property, which is a reference to a DOM object that you can load an external XML file into. Second, the object exposes a recordset property that represents a row‑by‑row view of the XML tree. With these two APIs, you can either let the browser automatically bind XML nodes to HTML tags or take full programmatic control with JavaScript.
Because XML‑DSO is an ActiveX control, it only runs in Internet Explorer 4 and later. If you need cross‑browser support, you would normally use a JavaScript XML parser or fetch the XML via AJAX. For legacy projects that still rely on IE, however, XML‑DSO remains a quick way to turn an XML file into a formatted web page.
In the next section we’ll see how the control can be used to extract data from XML code that lives inside the page itself – a technique known as an XML data island. This approach eliminates the need for external files and is useful when you have a small set of data embedded directly in the HTML.
Binding Data from XML Data Islands
An XML data island is a block of XML markup placed directly inside an HTML document. It is wrapped with an <xml> element and can be referenced by other tags in the page using the datasrc and datafld attributes. Because the XML‑DSO automatically parses these islands, you don't need to instantiate it manually – the browser does it behind the scenes when it encounters the <xml> element.
Consider this simple example. The page contains an XML data island that holds a list of members, each with a name and a sex tag. Two <SPAN> elements bind to the first <name> and <sex> nodes:
When the page loads, the first <span> shows “Premshree Pillai” and the second displays “male.” The browser automatically walks the XML tree, finds the first node that matches the datafld value, and injects its text content into the element. This method is straightforward but limited: it always returns the first occurrence of each node. If you need to display multiple records, you’ll need to use a table and let the browser iterate over the node set.
The table example demonstrates how the <TABLE> element can render every <member> node. Inside the table, each cell contains a <DIV> that binds to a child node. Because the table is bound to the same data source, the browser automatically repeats the row for each <member> element, producing a neatly formatted list:
With this technique you can turn any XML data island into a fully populated table without writing a single line of JavaScript. It works well for static pages or when the data set is small enough to embed directly into the HTML. For larger or dynamic data, you’ll want to load XML from an external source, which we cover next.
Loading and Displaying External XML Files
When your XML data resides outside the page, the XML‑DSO must be instructed to load it explicitly. This step is done through JavaScript, but the HTML markup for data binding remains the same. The workflow involves creating the control, loading the XML file into its XMLDocument, and then binding the data to table cells or other elements.
First, place the <OBJECT> element in the page. Give it an id that you’ll reference later. Keep the dimensions at zero to keep the object invisible:
Next, write a small JavaScript routine that loads the file. The routine obtains a reference to the object’s XMLDocument property and calls its load() method. The following snippet loads an XML file named example3.xml when the page finishes rendering:
Finally, you can bind the data to a table just like before. Each cell uses a <DIV> with a datafld attribute that points to the XML node you want to display. The table’s datasrc attribute references the object’s id:
The resulting page displays the message and URL contained in the XML file. The JavaScript load function is small but critical; without it, the object would contain no data and the binding would render nothing.





No comments yet. Be the first to comment!