An ND N Documn MBDDD N NDD Documn Uu NN N Ook K

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

XML Data Island

What is an XML Data Island?


An XML Data Island is an XML document embedded in a standard HTML/XHMTL document, usually within an <xml> tag. The initial page looks like this:

<html>
<head> ... </head> <body> <h1>Header</h1> <p>Text here ... </p> ... <xml id="scoreData"></xml> </body> </html>

XML Data Islands only works with Internet Explorer browsers.

XML Data Island Example in Internet Explorer


<html> <body> <xml id="cdcat"> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD> </CATALOG> </xml>

1|Page

<table border="1" datasrc="#cdcat"> <tr> <td><span datafld="ARTIST"></span></td> <td><span datafld="TITLE"></span></td> </tr> </table> </body> </html>

On internet explorer the page will be rendered as shown in Figure 1 below:

Figure 1: Internet Explorer Preview

The data is retrieve from the XML to be displayed in a table. In this example ARTIST and TITLE is being shown. However Firefox do not render the page the same way, as it has no support for XML Data Island.

Solution on Firefox
Possible solution to make use of XML Data Island on Firefox is to write a Javascript function to parse and render the XML data.

<html> <head> <script> varxmlDoc_modules; functionparseXML(){ varxmlNode = document.getElementById(' cdcat').innerHTML; xmlDoc_modules = loadXMLString(xmlNode); displayItems(); } functionloadXMLString(txt) { varxmlDoc; try //Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.loadXML(txt); //xmlDoc.encoding="UTF-8";

2|Page

returnxmlDoc; } catch(e) { try //Firefox, Mozilla, Opera, etc. { var parser=new DOMParser(); xmlDoc=parser.parseFromString(txt,"text/xml"); xmlDoc.encoding="UTF-8"; returnxmlDoc; } catch(e) {alert(e.message)} } return(null); }//end function loadXMLString functiondisplayItems() { vartable_str= "<br/><table border=1>"; varmodules_div = document.getElementById("div_modules"); varmodules_node=xmlDoc_modules.childNodes[0]; varlen = modules_node.childNodes.length; for (i=0; i<len ;i++) { try{ table_str = table_str + "<tr><td>"; var artist = modules_node.childNodes[i].childNodes[1].firstChild.nodeValue; table_str = table_str + artist +"</td><td>"; var title = modules_node.childNodes[i].childNodes[0].firstChild.nodeValue; table_str = table_str + title +"</td></tr>"; } catch(e){alert(e.message)} } table_str = table_str + "</table>"; modules_div.innerHTML = table_str; }//end function displayItems()

</script> </head> <body onload=parseXML();> <xml id="cdcat" style="display:none"> <catalog><cd><TITLES>Empire Burlesque</TITLES><ARTIST>Bob Dylan</ARTIST><COUNTRY>USA</COUNTRY><COMPANY>Columbia</COMPANY><PRICE>10.90</PRICE><YEAR>1985< /YEAR></cd><cd><TITLES>Hide your heart</ TITLES><ARTIST>Bonnie

3|Page

Tyler</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>CBS Records</COMPANY><PRICE>9.90</PRICE><YEAR>1988</YEAR></cd></catalog> </xml> <div id="div_modules"></div>

</body> </html>

4|Page

You might also like