Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching Function 2

Status
Not open for further replies.

TSMABob

Programmer
Jun 27, 2003
15
US
Hey everybody,

I'm rather new to the game, and I'm looking for a way to search through an XML document and display specific contents to a webpage.

Basically the setup is I have an XML "worksheet" that has a bunch of columns and rows set up. I have one webpage where the user submits a certain number, and I'd like to search for that number in one of the xml columns, and then find the corresponding name in the column next to it. This name and the number would then be posted onto the final page that can be printed out.

Does anyone know of a way to do this? Can it be done with javascript, or am I way out of my league here?
 
Think that you're going to need asp and the file system obect to accomplish that... I can help you with it or you can post in the the asp forum333.

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
If you want to do it in Javascript, you might want to try this:

Make 2 arrays. One array will have all the numbers in the column. The other array will have all the names of the second column.

Then, you use something like this:

var numbers = "..., ..., ...";
var names = "..., ... ,..."
var i = 0;

While (i < numbers.length)
{
if (numbers == input)
{
var wanted number = numbers[j];
var wanted name = names[j];
}
}
Then display wanted number and wanted name later on when desired. Just make sure that the two arrays match up.

- Skillz
 
Correction:

It should be

if (numbers[j] = input)

-Skillz
 
thanks for the idea skills.

The only problem is that i have upwards of 5000 entries to search through... and as much as I'd love to type those all in.... I was hoping to use my existing xml or excel document to contain the files.
 
Hi,

Why don't you use the javascript xmldom if client side or the asp xmldom serverside?

Whats the structure of the xml doc?

Mark
 
the xml doc was created by excel, but I'm cleaning it up now to get rid of all the useless crap.

right now it looks like:

<worksheet>
<table columncount=5>
<row>
<cell>
<data>number</data>
</cell>
</row>
<row>
<cell>
<data>name</data>
</cell>
</row>
<row>.....

</table>
</worksheet>

So there's a lot of nested information that I'm having a hard time getting through. All the information (name) that I need is always in the immediately following cell though (if there were a way to index through the xml, like you can in an array)
 
Are you sure it's not more like, Can you double check
<worksheet>
<table columncount=5>
<row>
<cellA>
<data>number</data>
</cellA>
<cellB>
<data>name</data>
</cellB>
</row>
<row>
<cellA>
<data>number</data>
</cellA>
<cellB>
<data>name</data>
</cellB>
</row>
<row>.....
</table>
</worksheet>

Else it means that every odd row has the number and every even has the name, which can still be done.
 
yeah, I guess I got a little excited typing!

the cells are not specified by cellA or cellB, but there is more than one cell in each row.

<worksheet>
<table columncount=5>
<row>
<cell>
<data>number</data>
</cell>
<cell>
<data>name</data>
</cell>
</row>
...
</worksheet>
 
Ok, you need to use the xmlDom for this.

I used it with an xml file called messages.xml that consisted of the following:

messages.xml
---------------

<?xml version=&quot;1.0&quot;?>
<worksheet>
<table>
<row>
<cell>
<data>1</data>
</cell>
<cell>
<data>name1</data>
</cell>
</row>
<row>
<cell>
<data>2</data>
</cell>
<cell>
<data>name2</data>
</cell>
</row>
<row>
<cell>
<data>3</data>
</cell>
<cell>
<data>name3</data>
</cell>
</row>
</table>
</worksheet>

----------------

end of xml file

----------------

This is the code for asp file using javascript

----------------

<html>
<head>
<title></title>
</head>
<body>
<script language=&quot;javascript&quot;>
searchNumber = 2
var source = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
source.async = false;
if( !source.load( &quot;message.xml&quot; ) )
{
//if the xml dosen't load execute the 4 alert lines
alert( source.parseError.reason );
alert( source.parseError.linepos );
alert( source.parseError.srcText );
alert( source.text );
}
else
{
var rootnode = source.documentElement.childNodes(0);
var numRows = rootnode.parentNode.childNodes(0).childNodes.length;
var numVal = 0;
var nameVal = 0;

i = 0
do
{
numVal = rootnode.childNodes(i).childNodes(0).childNodes(0).childNodes(0).nodeValue;

if(numVal = searchNumber)
{
nameVal = rootnode.childNodes(i+1).childNodes(1).childNodes(0).childNodes(0).nodeValue;
i = numRows;
}
i++
}
while (i < numRows)

document.write(numVal);
document.write(&quot;<br>&quot;);
document.write(nameVal);
}
</script>
</body>
</html>

------------------

end of asp code

searchNumber is the number being search for

numRows is assigned the number of rows in the file.

The Output
-----------
numVal = number being searched for
nameVal = Corrosponding value for numVal.

There you go,

Mark
 
wow... thank you so much Mark, you've been awesomely helpful!

I'll give that a shot.

Thank you again.
 
hrm... having a little problem.

It always finds the same value. I went back and used your example. If you set &quot;searchNumber&quot; = 3, it still comes back with name2, if you use 1, still name2.
 
it has something to do with the line:

nameVal = rootnode.childNodes(i+1).childNodes(1).childNodes(0).childNodes(0).nodeValue;


when I took away the i+1, it always comes back with &quot;name1&quot;
 
got it!!!

if(numVal == searchNumber)
instead of
if(numVal = searchNumber)

it was assigning the value rather than checking.

Thank you SOOOO much for your help though, I never could have figured that one out on my own.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top