javascript xml parser question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • annoyingmouse2002

    #1

    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.XMLDO M");

    /* 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="f alse";
    // See below
    xmlDoc.onreadys tatechange=veri fy;
    // Now load the file
    xmlDoc.load(xml File);
    // Gives us a nice name to use in subsequent code
    xmlObj=xmlDoc.d ocumentElement;
    }

    function verify()
    {
    /* LOADING (1) The load is in progress—readin g 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—readi ng 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.readySt ate != 4)
    {
    return false;
    }
    }

    // Gets the specified element content from a collection of
    elements
    function getElementConte nt(marker, contents)
    {
    for(b=0; b<marker.length ; b++)
    {
    if(marker(b).ta gName==contents )
    {
    return marker(b).first Child.text;
    }
    }
    }

    // Gets the specified attribute content from a specified element
    from a collection of elements
    function getAttributeCon tent(marker, contents, element)
    {
    for(b=0; b<marker.length ; b++)
    {
    if(marker(b).ta gName==contents )
    {
    return marker(b).getAt tribute(element );
    }
    }
    }


    // Doesn't matter what the file is called, either XML or OWL
    will do.
    loadXML("NHSTru st.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.childN odes.length; a++)
    {
    // ...filter out the employee elements and work on them.
    if(xmlObj.child Nodes(a).tagNam e=="employee")
    {
    // Just to make life easier for us
    var xmlWorking=xmlO bj.childNodes(a );
    eID[f]=xmlWorking.get Attribute("rdf: ID");
    ename[f]=getElementCont ent(xmlWorking. childNodes,"nam e");
    etitle[f]=getElementCont ent(xmlWorking. childNodes, "title");
    edetails[f]=getElementCont ent(xmlWorking. childNodes,
    "details");
    esupID[f]=getAttributeCo ntent(xmlWorkin g.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+"Titl e</th><th"+h+"Cont act
    Details");
    document.write( "</th><th"+h+"Supe rior's ID</th>");
    document.write( "</tr></thead><tbody>") ;
    for(g=0;g<eID.l ength;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="htt p://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="employe e"/>
    <owl:ObjectProp erty rdf:ID="superio r">
    <rdfs:comment>E mployee's superior</rdfs:comment>
    <rdfs:domain rdf:resource="# employee"/>
    <rdf:type rdf:resource="h ttp://www.w3.org/2002/07/owl#FunctionalP roperty"/>
    </owl:ObjectPrope rty>
    <owl:DatatypePr operty rdf:ID="details ">
    <rdfs:domain rdf:resource="# employee"/>
    <rdf:type rdf:resource="h ttp://www.w3.org/2002/07/owl#FunctionalP roperty"/>
    <rdfs:range rdf:resource="h ttp://www.w3.org/2001/XMLSchema#strin g"/>
    <rdfs:comment>C ontact details of employee</rdfs:comment>
    </owl:DatatypePro perty>
    <owl:DatatypePr operty rdf:ID="title">
    <rdfs:range rdf:resource="h ttp://www.w3.org/2001/XMLSchema#strin g"/>
    <rdfs:domain rdf:resource="# employee"/>
    <rdfs:comment>T itle of employee</rdfs:comment>
    <rdf:type rdf:resource="h ttp://www.w3.org/2002/07/owl#FunctionalP roperty"/>
    </owl:DatatypePro perty>
    <owl:Functional Property rdf:ID="name">
    <rdfs:comment>N ame of the employee</rdfs:comment>
    <rdf:type rdf:resource="h ttp://www.w3.org/2002/07/owl#DatatypePro perty"/>
    <rdfs:domain rdf:resource="# employee"/>
    <rdfs:label xml:lang="en">C ontact details</rdfs:label>
    <rdfs:range rdf:resource="h ttp://www.w3.org/2001/XMLSchema#strin g"/>
    </owl:FunctionalP roperty>
    <employee rdf:ID="H_Wards _and_Day_Servic e_Manager">
    <name>Erik Straker</name>
    <details>0122 3 604031</details>
    <superior rdf:resource="# Area_Director_H untingdon"/>
    <title>Wards &amp; Day Service Manager</title>
    </employee>
    <employee rdf:ID="Area_Di rector_Huntingd on">
    <details>0122 3 601298</details>
    <superior rdf:resource="# Chief_Executive "/>
    <name>Jessie Post</name>
    <title>Area Director Huntingdon</title>
    </employee>
    <employee rdf:ID="Chief_E xecutive">
    <details>0122 3 602600</details>
    <title>Chief Executive</title>
    <name>Eve Grindstaff</name>
    <superior rdf:resource="# Chairman"/>
    </employee>
    <employee rdf:ID="Chairma n">
    <details>0122 3 602301</details>
    <name>Alejand ra Bizzell</name>
    <title>Chairman </title>
    </employee>
    </rdf:RDF>
    <!-- Created with Protege (with OWL Plugin 1.0, Build 72)
    http://protege.stanford.edu -->
  • Martin Honnen

    #2
    Re: javascript xml parser question.



    annoyingmouse20 02 wrote:

    [color=blue]
    > I've been
    > using MSXML to parse an OWL but would like to use a different
    > solution.[/color]

    You can use MSXML but preferably on the server with ASP as that way your
    page will work in browsers other than IE/Win.
    [color=blue]
    > 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.[/color]

    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


    --

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

    Comment

    • annoyingmouse2002

      #3
      Re: javascript xml parser question.

      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 <mahotrash@yaho o.de> wrote in message news:<40e91c24@ olaf.komtel.net >...[color=blue]
      > annoyingmouse20 02 wrote:
      >
      >[color=green]
      > > I've been
      > > using MSXML to parse an OWL but would like to use a different
      > > solution.[/color]
      >
      > You can use MSXML but preferably on the server with ASP as that way your
      > page will work in browsers other than IE/Win.
      >[color=green]
      > > 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.[/color]
      >
      > 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/[/color]

      Comment

      • Martin Honnen

        #4
        Re: javascript xml parser question.



        annoyingmouse20 02 wrote:

        [color=blue]
        > 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...?[/color]

        Check here:




        --

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

        Comment

        • annoyingmouse2002

          #5
          Re: javascript xml parser question.

          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.owne rDocument;
          loadFile();
          }

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

          function getData(data){
          if (data.success){
          var xml = parseXML(data.c ontent).firstCh ild;
          var employees = xml.getElements ByTagName("empl oyee");
          alert('Loading has succeeded. There are '+employees.len gth+'
          employees');
          for(a=0;a<emplo yees.length;a++ ){
          alert(a+' = '+employees.ite m(a).getAttribu te('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 <mahotrash@yaho o.de> wrote in message news:<40ea934a$ 1@olaf.komtel.n et>...[color=blue]
          > annoyingmouse20 02 wrote:
          >
          >[color=green]
          > > 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...?[/color]
          >
          > 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[/color]

          Comment

          Working...