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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search an xml file

Status
Not open for further replies.

tyutghf

Technical User
Apr 12, 2008
258
GB
this is my first foray into xml and I am struggling to find anything on the web relating to what I want to do.

I have an xml file of a format

<products>
<product>
<name>product1</name>
<desc>this is product 1 description</desc>
<price>this is product 1 price</price>
<image>this is an image of product 1</image>
</product>
</products>

I need to search the 'name' node and output a list of results that contains the name, price and image.

I have found many articles that output the entire xml or search on name and only output name, but I need to output other fields too.

Here is what I have so far

Code:
Dim name 
Dim xmlDoc 
Dim rootNode 
strSearch= lcase(request.form("strKeywords")) 
response.write strSearch  
set xmlDoc=Server.CreateObject("Microsoft.XMLDOM") xmlDoc.async="false"   
xmlDoc.load(Server.MapPath("file.xml"))  
Set rootNode = xmlDoc.documentElement 
If rootNode.hasChildNodes() Then   
For Each pageNode in rootNode.childNodes     
For Each propertyNode in pageNode.childNodes 	 
If propertyNode.nodeName = "name" Then   
If InStr(lcase(propertyNode.Text), strSearch) Then 

Response.write "" & propertyNode.Text & ""   
End If 
End If     
Next   
Next 
End If

This will open my xml and search the name column for the search term the user entered but only output the name field, how do I output other fields such as price and image also?

Thanks
 
Not the best, just don't want to change much from yours.
[tt]
'etc etc
If InStr(lcase(propertyNode.Text), strSearch) Then
Response.write "" & propertyNode.Text & ""
[blue]'dim oparent
set oparent=propertyNode.parentnode
for each onode in oparent.childnodes
response.write "name: " & onode.nodename & "; " & " text: " & onode.text
next
set oparent=nothing[/blue]
End If
'etc etc
[/tt]
 
Will that output all of the child nodes of product? There are many more than in my example and I only need a few of them :eek:)

I bet this xml lark is quite good when I manage to get my head around it haha
 
Put some conditionals to wrap around the response.write if you want to filter out specific nodes.
 
Using the above I get the error

Response object error 'ASP 0251 : 80004005'

Response Buffer Limit Exceeded

/results.asp, line 0

Execution of the ASP page caused the Response Buffer to exceed its configured limit.

 
If you don't add the blue block, you don't get error?
 
I have performed a search when only 1 result is returned and I get the error with the new code but not without it. At first I thought infinite loop but can`t see one.
 
The xml file is 5MB in size so I removed all entries apart from the one I search on which make it very small size and your code works.

It seems that the extra code looping through the nodes is a push to far but without that code just going as far as the name field is ok on the original 5MB file.
 
>No
and then you add that many qualified statement... It would be hard pressed to have one to believe in. Marginal resources needed to load up that extra single node (the parentnode) and it exceed the limit? you bet.
 
I am have aded the following to only grab out the nodes I need to output to the page

Code:
if onode.nodename = "name" then response.write "<div class='box'><h2>" & onode.text & "</h2>" & vbcrlf
															if onode.nodename = "imageUrl" then response.write "<img src='images/" & onode.text & "'><div class='clr'></div>" & vbcrlf
															if onode.nodename = "description" then response.write "" & onode.text & "" & vbcrlf
															if onode.nodename = "productUrl" then response.write "<div class='read'><a href='" & onode.text & "'>more</a></div></div><div class='clr'></div>" & vbcrlf

However when it is printed to the page it is coming out in the wrong order!?!?! i.e.

<div class='box'><h2>prod name</h2>
<div class='read'><a href='link.html'>more</a></div></div><div class='clr'></div>
<img src='images/logo.jpg'><div class='clr'></div>
Description here


The image tag should be after the header tag and the more link is in the wrong place. How come?
 
That is not the robust way to write out html. The order of picking the nodes up is not guaranteed, in principle, but, often time according to the document order.
[tt]
sheader=""
simageUrl=""
sdescription=""
sproductUrl=""
strail="<div class='clr'></div>"

select case onode.name
case "name"
sheader="<h2>" & onode.text & "</h2>"
case "imageUrl"
simageUrl="<img src='images/" & onode.text & "'/>" & strail
case "description"
sdescription=onode.text
case "productUrl"
sproductUrl="<div class='read'><a href='" & onode.text & "'>more</a></div>
case else
end select
response.write [blue]"<div class='box'>"[/blue] & sheader & vbcrlf & _
simageUrl & vbcrlf & sdescription & vbcrlf & _
sproductUrl & [blue]"</div>"[/blue] & strail & vbcrlf
[/tt]
Always, at all times, make the balance of tags evident (no redistribution of open and close tag in different components, like the wrapper div with class="box"). Furthermore, keep the order of rendering under control. If the above is still not exactly what you want, you can shift the component around, but this time each component is clearly delineated.
 
A typo here.

[tt]case "productUrl"
sproductUrl="<div class='read'><a href='" & onode.text & "'>more</a></div>[red]"[/red][/tt]
 
Thanks for your help, I noticed a few other errors which I corrected but here is the code I have now

Code:
Dim name
Dim xmlDoc
Dim rootNode
name= lcase(request.querystring("letters"))
'response.write name

set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false" 
 xmlDoc.load(Server.MapPath("samplefile.xml"))

Set rootNode = xmlDoc.documentElement
If rootNode.hasChildNodes() Then
  For Each pageNode in rootNode.childNodes
    For Each propertyNode in pageNode.childNodes
	 If propertyNode.nodeName = "name" Then
                       

  If InStr(lcase(propertyNode.Text), name) Then
    'Response.write "" & propertyNode.Text & "<br />"
				
				
				set oparent=propertyNode.parentnode
    for each onode in oparent.childnodes
				
				
sheader=""
simageUrl=""
sdescription=""
sproductUrl=""
strail="<div class='clr'></div>"

select case onode.nodename
case "name"
    sheader="<h2>" & onode.text & "</h2>"
case "imageUrl"
    simageUrl="<img src='images/" & onode.text & "'/>" & strail
case "description"
    sdescription=onode.text
case "productUrl"
    sproductUrl="<div class='read'><a href='" & onode.text & "'>more</a></div>"
case else
end select
response.write "<div class='box'><div class='percent'>Up to 7%</div>" & sheader & vbcrlf & _
    simageUrl & vbcrlf & sdescription & vbcrlf & _
    sproductUrl & "</div>" & strail & vbcrlf
				
				next
		end if


																							
																							
																							
																							
End If
    Next
  Next
End If

It should be picking out just one record when I do my search but here is what it is outputting

Code:
																	<div class='box'><div class='percent'>Up to 7%</div><h2>prod name</h2>


</div><div class='clr'></div>
<div class='box'><div class='percent'>Up to 7%</div>


<div class='read'><a href='link.html'>more</a></div></div><div class='clr'></div>
<div class='box'><div class='percent'>Up to 7%</div>
<img src='images/logo.jpg'/><div class='clr'></div>

</div><div class='clr'></div>
<div class='box'><div class='percent'>Up to 7%</div>

Sony psp hand held console
</div><div class='clr'></div>
<div class='box'><div class='percent'>Up to 7%</div>


</div><div class='clr'></div>
 
well, you ask for this kind of help? with the xml in front of you and nobody else here? with the logic already clearly layout?
 
Sorry I do not understand your last reply
 
I understand what you have done in the code but cannot work out why after the header tag it prints this part

</div><div class='clr'></div>
<div class='box'><div class='percent'>Up to 7%</div>

instead of simageUrl and then after simageUrl it does the above again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top