using DOM to parse a XML file...

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

    #1

    using DOM to parse a XML file...

    hello,

    I need a little guidance here...
    I am tring to parse out an XML file, but with some restrictions.

    here is my XML FILE:
    ---------XML-----------
    <XML>
    <Cam name="01">
    <loc>Newport</loc>
    <lane des="ln1">
    <spd>20</spd>
    <ano>44</ano>
    </lane>
    <lane des="ln2">
    <spd>55</spd>
    <ano>11</ano>
    </lane>
    </Cam>
    <Cam name="02">
    <loc>Huntington </loc>
    <lane des="ln1">
    <spd>49</spd>
    <ano>99</ano>
    </lane>
    <lane des="ln2">
    <spd>88</spd>
    <ano>00</ano>
    </lane>
    </Cam>
    </XML>
    -----------------------

    What I want to do is collect all the data from each <lane> node and
    place it into a string each value deliminated by a ',' .

    example:
    var c1ln1Data =("locNode.lane Node.spdNode.va lue,locNode.lan eNode.anoNode.v alue")

    But I only want to get this information if the camNode attribute
    matches a value that I pass in....

    i.e.:
    <Cam name="01">.... if the 'name' value matches (var myNum) then parse
    information that is a child of Cam01 *Only* then exit.

    Any help is greatly appreciated!
    Thanks.
  • Martin Honnen

    #2
    Re: using DOM to parse a XML file...



    Charlie T wrote:

    [color=blue]
    > I am tring to parse out an XML file, but with some restrictions.
    >
    > here is my XML FILE:
    > ---------XML-----------
    > <XML>[/color]

    You shouldn't name an element XML, that is a reserved name.
    [color=blue]
    > <Cam name="01">
    > <loc>Newport</loc>
    > <lane des="ln1">
    > <spd>20</spd>
    > <ano>44</ano>
    > </lane>
    > <lane des="ln2">
    > <spd>55</spd>
    > <ano>11</ano>
    > </lane>
    > </Cam>
    > <Cam name="02">
    > <loc>Huntington </loc>
    > <lane des="ln1">
    > <spd>49</spd>
    > <ano>99</ano>
    > </lane>
    > <lane des="ln2">
    > <spd>88</spd>
    > <ano>00</ano>
    > </lane>
    > </Cam>
    > </XML>
    > -----------------------
    >
    > What I want to do is collect all the data from each <lane> node and
    > place it into a string each value deliminated by a ',' .
    >
    > example:
    > var c1ln1Data =("locNode.lane Node.spdNode.va lue,locNode.lan eNode.anoNode.v alue")
    >
    > But I only want to get this information if the camNode attribute
    > matches a value that I pass in....
    >
    > i.e.:
    > <Cam name="01">.... if the 'name' value matches (var myNum) then parse
    > information that is a child of Cam01 *Only* then exit.[/color]

    Not all browsers support loading and parsing of XML documents, here is
    an example that has been tested to work with Netscape 7.2, IE 6, Opera
    7.60 Preview 1 and should work with Netscape 6/7, Mozilla, IE5/6/Win and
    maybe Safari 1.2:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>loadin g and parsing an XML document</title>
    <script type="text/javascript">
    function loadAndParseXML (url, processHandler) {
    var httpRequest;
    if (typeof XMLHttpRequest != 'undefined') {
    httpRequest = new XMLHttpRequest( );
    }
    else if (typeof ActiveXObject != 'undefined') {
    httpRequest = new ActiveXObject(' Microsoft.XMLHT TP');
    }
    if (httpRequest) {
    httpRequest.ope n('GET', url, true);
    httpRequest.onr eadystatechange = function () {
    if (httpRequest.re adyState == 4 &&
    httpRequest.sta tus == 200) {
    processHandler( httpRequest.res ponseXML);
    }
    };
    httpRequest.sen d(null);
    }
    }

    function getInnerText (node) {
    var innerText = '';
    if (node.nodeType == 3) {
    return node.nodeValue;
    }
    else {
    for (var i = 0; i < node.childNodes .length; i++) {
    innerText += getInnerText(no de.childNodes[i]);
    }
    return innerText;
    }
    }

    function searchButtonHan dler (form) {
    loadAndParseXML (
    form.url.value,
    function (xmlDocument) {
    var camElements = xmlDocument.get ElementsByTagNa me('Cam');
    var result = ''
    for (var i = 0; i < camElements.len gth; i++) {
    if (camElements[i].getAttribute(' name') == form.camName.va lue) {
    var lanes = camElements[i].getElementsByT agName('lane')
    for (var j = 0; j < lanes.length; j++) {
    var els = lanes[j].getElementsByT agName('*');
    for (var k = 0; k < els.length; k++) {
    result += getInnerText(el s[k]) + ', ';
    }
    }
    result = result.substrin g(0, result.length - 2);
    }
    }
    if (!result) {
    result = 'Not found.';
    }
    form.output.val ue = result;
    }
    );
    }
    </script>
    </head>
    <body>
    <form action="" onsubmit="retur n false;" name="gui">
    <p>
    <label>
    Enter URL of XML document to search:
    <input type="text" name="url" value="test2004 091001.xml">
    </label>
    </p>
    <p>
    <label>
    Enter name to search:
    <input type="text" name="camName">
    </label>
    <input type="button" value="search"
    onclick="search ButtonHandler(t his.form);">
    </p>
    <p>
    <label>
    found the following:
    <br>
    <textarea name="output" rows="10" cols="80"></textarea>
    </label>
    </p>
    </form>
    </body>
    </html>

    With other browsers you need to implement the search on the server.

    --

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

    Comment

    Working...