Charlie T wrote:
I am tring to parse out an XML file, but with some restrictions.
here is my XML FILE:
---------XML-----------
<XML>
You shouldn't name an element XML, that is a reserved name.
<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.laneNode.spdNode.value,locNode.laneNode .anoNode.value")
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.
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>loading 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.XMLHTTP');
}
if (httpRequest) {
httpRequest.open('GET', url, true);
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 &&
httpRequest.status == 200) {
processHandler(httpRequest.responseXML);
}
};
httpRequest.send(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(node.childNodes[i]);
}
return innerText;
}
}
function searchButtonHandler (form) {
loadAndParseXML(
form.url.value,
function (xmlDocument) {
var camElements = xmlDocument.getElementsByTagName('Cam');
var result = ''
for (var i = 0; i < camElements.length; i++) {
if (camElements[i].getAttribute('name') == form.camName.value) {
var lanes = camElements[i].getElementsByTagName('lane')
for (var j = 0; j < lanes.length; j++) {
var els = lanes[j].getElementsByTagName('*');
for (var k = 0; k < els.length; k++) {
result += getInnerText(els[k]) + ', ';
}
}
result = result.substring(0, result.length - 2);
}
}
if (!result) {
result = 'Not found.';
}
form.output.value = result;
}
);
}
</script>
</head>
<body>
<form action="" onsubmit="return false;" name="gui">
<p>
<label>
Enter URL of XML document to search:
<input type="text" name="url" value="test2004091001.xml">
</label>
</p>
<p>
<label>
Enter name to search:
<input type="text" name="camName">
</label>
<input type="button" value="search"
onclick="searchButtonHandler(this.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/