Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries [Thread Prev] >Thread Next - Re: javascript xml parser question. javascript xml parser question.To: NULL Date: 7/4/2004 4:59:00 PM 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 & 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 --> | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
