473,387 Members | 1,722 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,387 software developers and data experts.

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

Any help is greatly appreciated!
Thanks.
Jul 23 '05 #1
1 1987


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

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

Similar topics

0
by: atlantis | last post by:
Hi, I have a very strange problem with xsl:import when usig RELATIVE path on AIX 5.2 server. I have two XSL files in the same directory: "ists_xslt3.xsl" and "ists_xslt3_layout.xsl". This...
2
by: Clay Luther | last post by:
I had been having good luck writing stored procs in C++ with Postgres. But, today, I needed to include the SPI interface to help a trigger along, aka #include "executor/spi.h" and this is...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
18
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
5
by: Mukesh | last post by:
Hi i want to use AJAX.net in my Existing Application I have already installed the ajax .net ..net 3.0 and using VS 2005 in the old application i have added a new web form then script manager...
5
by: moddster | last post by:
Hi Guys. I am a newbie to perl and need some help with a problem. PROBLEM: I have to parse an HTML file and get rid of all the HTML tags and count the number of sumbissions a person has through...
25
by: Jon Slaughter | last post by:
I have some code that loads up some php/html files and does a few things to them and ultimately returns an html file with some php code in it. I then pass that file onto the user by using echo. Of...
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.