javascript xml parser question.

  • Thread starter annoyingmouse2002
  • Start date
A

annoyingmouse2002

Hi there,

sorry if this a long post but I'm really just starting out. I've been
using MSXML to parse an OWL but would like to use a different
solution. Basically it reads the OWL (Based on XML) and puts values in
a number of arrays and then puts the contents of the array in a HTML
table. I'd like to keep the array structure. I've checked out all
sorts of different javascript parsers but have not met with a great
deal of success with any of them and I'd be grateful for any advice.
I'll include the two files below and would be grateful for any advice.

Thanks in advance,

Dom

--HTML File--
<!-- Start of document -->
<html>
<head>
<title>Using MSXML to look through the Ontology</title>
<script type="text/javascript">

/* Once you have installed IE, the parser is available to
scripts, both inside HTML documents and inside
ASP files. The parser features a language-neutral programming
model that supports:
+ JavaScript, VBScript, Perl, VB, Java, C++ and more
+ W3C XML 1.0 and XML DOM
+ DTD and validation */
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

/* Actually, just the last two lines of the function are enough
to load the XML file. The previous two
lines are written to ensure that the JavaScript functions
that we may use later to manipulate the XML
file data, does not perform any function on an uninitialized
object. Thus the function verify is
called.*/
function loadXML(xmlFile)
{
/* By default, the loading and parsing of an XML file occurs
asynchronously. To load the XML file
synchronously, set the async property to False. This forces
the loading of the entire XML document
before processing continues.*/
xmlDoc.async="false";
// See below
xmlDoc.onreadystatechange=verify;
// Now load the file
xmlDoc.load(xmlFile);
// Gives us a nice name to use in subsequent code
xmlObj=xmlDoc.documentElement;
}

function verify()
{
/* LOADING (1) The load is in progress—reading persisted
properties, but not yet parsing data. For
readyState definitions, data should be considered
equivalent to binary large object (BLOB)
properties.
LOADED (2) Reading of the persisted properties
completed—reading and parsing data, but the object
model is not yet available.
INTERACTIVE (3) Some data has been read and parsed, and the
object model is now available on the
partially retrieved data set. Although the object model is
available during this state, it is
read-only.
COMPLETED (4) The document has been completely loaded,
successfully or unsuccessfully.*/
if (xmlDoc.readyState != 4)
{
return false;
}
}

// Gets the specified element content from a collection of
elements
function getElementContent(marker, contents)
{
for(b=0; b<marker.length; b++)
{
if(marker(b).tagName==contents)
{
return marker(b).firstChild.text;
}
}
}

// Gets the specified attribute content from a specified element
from a collection of elements
function getAttributeContent(marker, contents, element)
{
for(b=0; b<marker.length; b++)
{
if(marker(b).tagName==contents)
{
return marker(b).getAttribute(element);
}
}
}


// Doesn't matter what the file is called, either XML or OWL
will do.
loadXML("NHSTrust.owl");

/* We'll construct 5 new arrays with the index having the same
number for each employee, this seems the
easiest way in which to access the pertinent data from the
XML file and we'll work on this data
rather than the file itself.*/
var eID=new Array();
var ename=new Array();
var etitle=new Array();
var edetails=new Array();
var esupID=new Array();

// Initialize the index for the five arrays created above
var f=0;
// Work through the whole document but...
for(a=0; a<xmlObj.childNodes.length; a++)
{
// ...filter out the employee elements and work on them.
if(xmlObj.childNodes(a).tagName=="employee")
{
// Just to make life easier for us
var xmlWorking=xmlObj.childNodes(a);
eID[f]=xmlWorking.getAttribute("rdf:ID");
ename[f]=getElementContent(xmlWorking.childNodes,"name");
etitle[f]=getElementContent(xmlWorking.childNodes, "title");
edetails[f]=getElementContent(xmlWorking.childNodes,
"details");
esupID[f]=getAttributeContent(xmlWorking.childNodes,
"superior", "rdf:resource");
// Increment "f" so that we can add the next employee's data
f++;
}
}

// This converts the information from the above arrays into a
pretty table
// Used merely to make the following code less verbose
var h=" style=\"text-align: left; vertical-align: top;
white-space: nowrap;\">";
document.write("<table style=\"text-align: left; width: 100%;\"
border=\"1\" cellspacing=\"2\"");
document.write("cellpadding=\"2\">");
document.write("<thead><tr><th"+h+"ID</th><th"+h+"Name</th><th"+h+"Title</th><th"+h+"Contact
Details");
document.write("</th><th"+h+"Superior's ID</th>");
document.write("</tr></thead><tbody>");
for(g=0;g<eID.length;g++)
{
document.write("<tr><td"+h);
document.write(eID[g]);
document.write("</td><td"+h);
document.write(ename[g]);
document.write("</td><td"+h);
document.write(etitle[g]);
document.write("</td><td"+h);
document.write(edetails[g]);
document.write("</td><td"+h);
document.write(esupID[g]);
document.write("</td></tr>");
}
document.write("</tbody></table>");

</script>
</head>
<!-- Empty body as the code gives the page its contents -->
<body/>
</html>
<!-- End of Document -->

--OWL File--
<?xml version="1.0"?>
<rdf:RDF xmlns="http://a.com/ontology#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:eek:wl="http://www.w3.org/2002/07/owl#"
xml:base="http://a.com/ontology">
<owl:Ontology rdf:about=""/>
<owl:Class rdf:ID="employee"/>
<owl:ObjectProperty rdf:ID="superior">
<rdfs:comment>Employee's superior</rdfs:comment>
<rdfs:domain rdf:resource="#employee"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/>
</owl:ObjectProperty>
<owl:DatatypeProperty rdf:ID="details">
<rdfs:domain rdf:resource="#employee"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/>
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:comment>Contact details of employee</rdfs:comment>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:ID="title">
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="#employee"/>
<rdfs:comment>Title of employee</rdfs:comment>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/>
</owl:DatatypeProperty>
<owl:FunctionalProperty rdf:ID="name">
<rdfs:comment>Name of the employee</rdfs:comment>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#DatatypeProperty"/>
<rdfs:domain rdf:resource="#employee"/>
<rdfs:label xml:lang="en">Contact details</rdfs:label>
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
</owl:FunctionalProperty>
<employee rdf:ID="H_Wards_and_Day_Service_Manager">
<name>Erik Straker</name>
<details>01223 604031</details>
<superior rdf:resource="#Area_Director_Huntingdon"/>
<title>Wards &amp; Day Service Manager</title>
</employee>
<employee rdf:ID="Area_Director_Huntingdon">
<details>01223 601298</details>
<superior rdf:resource="#Chief_Executive"/>
<name>Jessie Post</name>
<title>Area Director Huntingdon</title>
</employee>
<employee rdf:ID="Chief_Executive">
<details>01223 602600</details>
<title>Chief Executive</title>
<name>Eve Grindstaff</name>
<superior rdf:resource="#Chairman"/>
</employee>
<employee rdf:ID="Chairman">
<details>01223 602301</details>
<name>Alejandra Bizzell</name>
<title>Chairman</title>
</employee>
</rdf:RDF>
<!-- Created with Protege (with OWL Plugin 1.0, Build 72)
http://protege.stanford.edu -->
 
M

Martin Honnen

annoyingmouse2002 wrote:

I've been
using MSXML to parse an OWL but would like to use a different
solution.

You can use MSXML but preferably on the server with ASP as that way your
page will work in browsers other than IE/Win.
Basically it reads the OWL (Based on XML) and puts values in
a number of arrays and then puts the contents of the array in a HTML
table. I'd like to keep the array structure. I've checked out all
sorts of different javascript parsers but have not met with a great
deal of success with any of them and I'd be grateful for any advice.
I'll include the two files below and would be grateful for any advice.

What exactly is your question?
Have you tried your example in a browser and got error messages? Which
browser have you tried with?

As said, your first approach should be a server side solution. If you
really want to go client-side and don't know how to approach it then you
might want to check out Sarissa at
http://sarissa.sourceforge.net/
 
A

annoyingmouse2002

Hi Martin, thanks for getting back to me.

Basically I was looking at using another parser rather than the MS
one, and was looking for advice on what ones were available out there
for the client-side, specifically in terms of grabbing the data that I
have and presenting it in arrays. I haven't had a chance to look at
your suggestion yet but have also been looking at XML for Script along
with a number of other possible solutions. I'm hoping to keep the
processing off of the server for now but might alter this in the
future and the idea of using MSXML on the server isn't one that I've
come across before... do you know if this would work on a *nix server?

The program works a treat on my set up which is IE on XP with the
MSXML parser installed but I get an error if running on a machine
without the MSXML parser because it's an activeX thingy is pulls up an
active X error, which I suppose is fair enough ;-)

Eventually I'll be using the arrays to do some calculations so I need
that structure. A bit of the output will be in the form of an SVG so I
was looking at using the adobe SVG plug-in's abilities to parse
XML...?

Thanks again,

Dom
 
A

annoyingmouse2002

Thanks Martin, I've cobled the following code but am unsure of the
syntax for the parseXML engine on the adobe plugin...

<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width='100%' height='600' onload='dessin(evt)'>
<script><![CDATA[
var svgdoc;

function dessin(evt){
svgdoc = evt.target.ownerDocument;
loadFile();
}

// Load the XML Data
function loadFile(){
getURL("NHSTrust.owl", getData);
}

function getData(data){
if (data.success){
var xml = parseXML(data.content).firstChild;
var employees = xml.getElementsByTagName("employee");
alert('Loading has succeeded. There are '+employees.length+'
employees');
for(a=0;a<employees.length;a++){
alert(a+' = '+employees.item(a).getAttribute('rdf:ID'));
}
}
else{
alert('Loading has failed');
}
}

]]></script>
</svg>

I know that it's merely a question of moving up and down the DOM but
when I did this using MSXML the functions were named differently.

Cheers, Dom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top