473,382 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

javascript xml parser question.

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:owl="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 -->
Jul 20 '05 #1
4 4235


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/

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
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


Martin Honnen <ma*******@yahoo.de> wrote in message news:<40******@olaf.komtel.net>...
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/

Jul 20 '05 #3


annoyingmouse2002 wrote:

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...?


Check here:
http://www.protocol7.com/svg-wiki/in...AdobeSVGViewer
http://www.protocol7.com/svg-wiki/index.php/ParseXml
http://www.protocol7.com/svg-wiki/index.php/GetUrl

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #4
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

Martin Honnen <ma*******@yahoo.de> wrote in message news:<40********@olaf.komtel.net>...
annoyingmouse2002 wrote:

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...?


Check here:
http://www.protocol7.com/svg-wiki/in...AdobeSVGViewer
http://www.protocol7.com/svg-wiki/index.php/ParseXml
http://www.protocol7.com/svg-wiki/index.php/GetUrl

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: S'fly | last post by:
Hi, I'm struggling with the famous "gatekeeper" a little. Normally, it opens in a popup window, and from there it goes on. But I like this script to open in an iframe, and not in a popup. It...
5
by: Lucian Sandor | last post by:
Hello everyone, While I'm a newbie here, I a not new to google, so please don't send me back, it would be useless. First of all I have to specify I am working on a Blogger.com template, therefore...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
2
by: David Virgil Hobbs | last post by:
Loading text strings containing HTML code into an HTML parser in a Javascript/Jscript I would like to know, how one would go about loading a text string containing HTML code, so as to be able to...
2
by: rbann11 | last post by:
Hi, I have a script that contains an XML string that will define how the page will be displayed. The XML string contains custom tags, with attributes and html tags. I will be replacing the...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
6
by: jackwootton | last post by:
Hello everyone, I understand that XML can be parsed using JavaScript using the XML Document object. However, it is possible to parse XHTML using JavaScript? I currently listen for DOMMutation...
0
by: UncleRic | last post by:
Environment: Mac OS X (10.4.10) on MacBook Pro I'm a Perl Neophyte. I've downloaded the XML::Parser module and am attempting to install it in my working directory (referenced via PERL5LIB env): ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.