Home page of Sandesh Karalkar
Yahoo IM status
In-memory RDF Updated
XUL Datepicker
Mozilla.org
Colorization tutorial
Photoshop India
Currency to word
Alibag Picnic
Cool Links
Pearls
Stunning faces
 

How to create In-memory RDF from remote XML

During one of my project, I faced a challenge to create In-Memory RDF database from a remote XML file. I tried lot of search queries on net, posted lot of newsgroup messages but nothing works.

After all hard-search I could manage this. Download file in-memory-rdf.zip
Contains two files window.xul and window.js. Run file window.xul (Note: You must run this file (window.xul) from any chrome application). You will see the tree containing data from my web server.

In-memory RDF example screenshot

-Download in-memory-rdf.zip
 [12-01-2004: Updated selectCustomer() function]

Instructions

  1. Download file in-memory-rdf.zip OR create two files window.xul and window.js from the code below.
  2. Keep this files in same directory in your any existing chrome application.
  3. Run window.xul from your existing chrome application.
      eg: chrome://yourapp/content/in-memory-rdf/window.xul

Here is the code for window.xul and window.js files. You can copy and paste the code from here.

Window.xul
<?xml version="1.0"?>
<!--
Contributed by Sandesh Karalkar (skexz@yahoo.co.in)
More hellp @ http://in.geocities.com/skexz
-->

<?xml-stylesheet href="chrome://global/skin" type="text/css"?>

<window id="in-mem" title="In-memory RDF example" onload="startup();"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
width="600"
height="400"
persist="screenX screenY sizemode">

<script type="application/x-javascript" src="window.js"/>

<!-- Customer tree -->
<tree id="bo-customer-tree" flex="1" datasources="rdf:null" ref="urn:root"
autostretch="always" seltype="single" flags="dont-build-content"
containment="http://www.bttlindia.com/RDF#subheadings"
enableColumnDrag="true" onselect="selectCustomer(event);">
<treecols>
<treecol id="customer-id" label="Customer ID"
flex="1" persist="width ordinal hidden sortActive sortDirection"
primary="true" class="sortDirectionIndicator" sortActive="true"
sortDirection="ascending" sort="rdf:http://www.bttlindia.com/rdf#id"/>
<splitter class="tree-splitter" />
<treecol id="customer-name" label="Name" flex="3"
persist="width ordinal hidden sortActive sortDirection" class="sortDirectionIndicator"
sortActive="true" sort="rdf:http://www.bttlindia.com/rdf#name"/>
<splitter class="tree-splitter" />
<treecol id="customer-phone" label="Phone" flex="1"
persist="width ordinal hidden sortActive sortDirection" class="sortDirectionIndicator"
sortActive="true" sort="rdf:http://www.bttlindia.com/rdf#pfone"/>
<splitter class="tree-splitter" />
<treecol id="customer-phiscal-number" label="Phiscal No." flex="1"
persist="width ordinal hidden sortActive sortDirection" class="sortDirectionIndicator"
sortActive="true" sort="rdf:http://www.bttlindia.com/rdf#fiscalnumber"/>
</treecols>

<template>
<rule>
<conditions>
<treeitem uri="?uri" />
<triple subject="?uri" predicate="http://www.bttlindia.com/RDF#subheadings" object="?subheadings" />
<member container="?subheadings" child="?subheading" />
</conditions>

<bindings>
<binding subject="?subheading" predicate="http://www.bttlindia.com/RDF#id" object="?id" />
<binding subject="?subheading" predicate="http://www.bttlindia.com/RDF#name" object="?name" />
<binding subject="?subheading" predicate="http://www.bttlindia.com/RDF#phone" object="?phone" />
<binding subject="?subheading" predicate="http://www.bttlindia.com/RDF#fiscalnumber" object="?fiscalnumber" />
</bindings>

<action>
<treechildren>
<treeitem uri="?subheading" >
<treerow>
<treecell label="?id"/>
<treecell label="?name"/>
<treecell label="?phone"/>
<treecell label="?fiscalnumber"/>
</treerow>
</treeitem>
</treechildren>
</action>

</rule>
</template>
</tree>
</window>

 

window.js
/*
Contributed by Sandesh Karalkar (skexz@yahoo.co.in)
http://in.geocities.com/skexz
*/


function startup() //Startup function.
{
createCustomerRDF('http://in.geocities.com/skexz/tuts/xul/in-memory/customers.xml');
}


function requestXMLdocument(URL) //requesting XML file from the remote location
{
var oXML=new XMLHttpRequest();
oXML.open("GET",URL,false);
oXML.send(null);
//Returning the document content to the XML parser function
return oXML.responseText;
}


function readXMLdocument(URL) //Parsing XML message.
{
xmlMessage = requestXMLdocument(URL);
this.parser = new DOMParser();
var xmlDOM = this.parser.parseFromString(xmlMessage, "text/xml");
//Returning the XML DOM.
return xmlDOM;
}


function createCustomerRDF(URL){ //Creating customers In-Memory RDF feed

var xmlDoc = readXMLdocument(URL);
var customers = xmlDoc.documentElement;
var customer = customers.getElementsByTagName("customer");


// get the XPCOM services we need to work with RDF's
var RDF =
Components.classes['@mozilla.org/rdf/rdf-service;1'].getService();
RDF = RDF.QueryInterface(Components.interfaces.nsIRDFService);

var RDFC = Components.classes['@mozilla.org/rdf/container;1'].getService();
RDFC = RDFC.QueryInterface(Components.interfaces.nsIRDFContainer);

var RDFCUtils =
Components.classes['@mozilla.org/rdf/container-utils;1'].getService();
RDFCUtils =
RDFCUtils.QueryInterface(Components.interfaces.nsIRDFContainerUtils);

var ds =
Components.classes["@mozilla.org/rdf/datasource;1?name=in-memory-datasource"].createInstance();
ds = ds.QueryInterface(Components.interfaces.nsIRDFDataSource);

// create our root nodes
var rootNode = RDF.GetResource("urn:root");
var seqNode = RDF.GetResource("urn:root:seq");

// insert the "top" of the tree, a Seq container
ds.Assert(rootNode,
RDF.GetResource("http://www.bttlindia.com/RDF#subheadings"),
seqNode, true);

RDFCUtils.MakeSeq(ds, seqNode);
RDFC.Init(ds, seqNode);

// get the tree and add the datasource
var tree = document.getElementById("bo-customer-tree");
tree.database.AddDataSource(ds);


// let's add some data.
for (var i = 0; i < customer.length; i++) {
var newURI = "urn:root:file:" + i;

// add the new element
RDFC.AppendElement(RDF.GetResource(newURI));

ds.Assert(RDF.GetResource(newURI),
RDF.GetResource("http://www.bttlindia.com/RDF#id"),
RDF.GetLiteral(customer[i].getAttribute("id")), true);

ds.Assert(RDF.GetResource(newURI),
RDF.GetResource("http://www.bttlindia.com/RDF#name"),
RDF.GetLiteral(customer[i].getAttribute("name")), true);

ds.Assert(RDF.GetResource(newURI),
RDF.GetResource("http://www.bttlindia.com/RDF#phone"),
RDF.GetLiteral(customer[i].getAttribute("phone")), true);

ds.Assert(RDF.GetResource(newURI),
RDF.GetResource("http://www.bttlindia.com/RDF#fiscalnumber"),
RDF.GetLiteral(customer[i].getAttribute("fiscalnumber")), true);
}

tree.builder.rebuild();
}

/* Updated on 1/12/04. */
function selectCustomer(event)
{
   // the tree is the target of the event
  var tree = event.target;
  var customerId =tree.view.getCellText(tree.currentIndex,"customer-id");
  var customerName =tree.view.getCellText(tree.currentIndex,"customer-name");
  alert("Customer ID : " + customerId +"\n"+
  "Customer name : "+ customerName +"");
}

 

 

Sandesh

 

Get Firefox - Take Back the Web    

Copyright (C) Sandesh Karalkar

1