Read XML using ASP

Discussion in 'C#' started by sapgaurav, Mar 10, 2009.

  1. #1
    i want to read XML using asp, i tried but i can not display propeer

    i can read data of xml but the problem is i want to link the title of all node

    linking is working but the first data of node is not getting linked also the first node link is going on next and next to next

    Following are the XML data

    <RESULTS>
    −
    <LISTING>
    <RANK>1</RANK>
    <TITLE>Jobs in Top Companies</TITLE>
    −
    <DESCRIPTION>
    200000+ Jobs, 20000 recruiters. Register now to apply free.
    </DESCRIPTION>
    <SITEHOST>www.yuvajobs.com</SITEHOST>
    −
    <LINK>www.yuvajobs.com
    </LINK>
    <ID1>LFPvAffVC0</ID1>
    </LISTING>
    −
    <LISTING>
    <RANK>2</RANK>
    <TITLE>Engineering Jobs</TITLE>
    −
    <DESCRIPTION>
    Engineering jobs. Over 200000 jobs to search from. Register now.
    </DESCRIPTION>
    <SITEHOST>www.indianfresher.com</SITEHOST>
    −
    <LINK>
    www.indianfresher.com
    </LINK>
    <ID1>LFPvAffVC1</ID1>
    </LISTING>
    </RESULTS>

    i want to read above xml file properly

    can anyone help me?

    if know kindly mail me the asp code at sapgaurav@yahoo.com
    Gaurav Jain
     
    sapgaurav, Mar 10, 2009 IP
  2. ludwig

    ludwig Notable Member

    Messages:
    2,253
    Likes Received:
    66
    Best Answers:
    0
    Trophy Points:
    225
    #2
    I use this one:

    <%
    rssParser "[COLOR="Red"]XML URL PUT IT HERE[/COLOR]"
    
    Sub rssParser(rssFile)
    
    Dim objXML, objRoot, objItems, c
    c = 0
    Set objXML = Server.CreateObject("Microsoft.XMLDOM")
    objXML.Async = False
    objXML.SetProperty "ServerHTTPRequest", True
    objXML.ResolveExternals = True
    objXML.ValidateOnParse = True
    objXML.Load(rssFile)
    
    If (objXML.parseError.errorCode = 0) Then
    Set objRoot = objXML.documentElement
    If IsObject(objRoot) = False Then
    response.Write "<h2>No Root Found in Rss File </h2>"
    End If
    
    Set objItems = objRoot.getElementsByTagName("item")
    If IsObject(objItems) = True Then
    Dim objItem, strFull
    For Each objItem in objItems
    		strTitle = objItem.selectSingleNode("title").Text
    								strTitle = replace(strTitle, "&amp;", "&")
    								strTitle = replace(strTitle, "&apos;", "&#39;")
    								strTitle = replace(strTitle, "&amp;apos;", "&#39;")
    								strTitle = replace(strTitle, "&amp;", "&")
    								strTitle = replace(strTitle, "&amp;&", "&")
    								strTitle = replace(strTitle, "?", "")
    								strTitle = replace(strTitle, vbcrlf, "<br />")
    		'On Error Resume Next 
    		strDesc = objItem.selectSingleNode("lead").Text
    								strDesc = replace(strDesc, "&amp;", "&")
    								strDesc = replace(strDesc, "&apos;", "&#39;")
    								strDesc = replace(strDesc, "&amp;apos;", "&#39;")
    								strDesc = replace(strDesc, "&amp;", "&")
    								strDesc = replace(strDesc, "&amp;&", "&")
    								strDesc = replace(strDesc, "?", "")
    								strDesc = replace(strDesc, vbcrlf, "<br />")
    		'On Error Resume Next
    		'strCategory = objItem.selectSingleNode("category").Text
    		'strCategory = replace(strCategory, "?", "&#39;")
    		'strCategory = replace(strCategory, "'", "&#39;")
    		
    		'On Error Resume Next
    		strLink = objItem.selectSingleNode("link").Text
    		'On Error Resume Next
    		strDate = objItem.selectSingleNode("pubDate").Text
    		'On Error Resume Next
    		strFull = objItem.selectSingleNode("full").Text
    		'On Error Resume Next
    		
    		set rsLink = Server.CreateObject("ADODB.Recordset")
    		set rsLinkID = Server.CreateObject("ADODB.Recordset")
    		strLanguage = "eng"
    		intID = cint(right(strLink,5))
    		strSQL="SELECT * FROM HAY_news WHERE newsid="&intID&" and lang='"&strLanguage&"' "
    		rsLink.Open strSQL, Global_DBConnection
    		if rsLink.BOF=true and rsLink.EOF=true then
    									'add process
    										strSQL="SELECT max(id) AS mymax FROM HAY_news"
    										rsLinkID.Open strSQL, Global_DBConnection
    										if len(trim(rsLinkID("mymax")))>0 then
    												intMax=rsLinkID("mymax")+1
    											else
    												intMax=1
    										end if
    										rsLinkID.Close
    										intStoraket = instr(strDate,",")
    										strDate = right(strDate,(len(strDate)-(intStoraket+1)))
    										strDate = left(strDate,(len(strDate)-4))
    										strDate = FormatDateTime(strdate,2) & " " & FormatDateTime(strdate,4)
    										strSQL="INSERT INTO HAY_news(id, newsid, pubdate, title, lang, url, agency, category) VALUES ("&intMax&", '"&intID&"', '"&strDate&"', '"&strTitle&"', '"&strLanguage&"', '"&strLink&"', 1, '"&strCategory&"'  )"
    										Global_DBConnection.Execute strSQL
    												Set fs = CreateObject("Scripting.FileSystemObject")
    												'Writing the news to the file
    												filename = server.mappath("news/" & cstr(intMax) & ".txt")
    												Set thisfile = fs.OpenTextFile(filename,2,True)
    													strFull = replace(strFull, "&amp;", "&")
    													strFull = replace(strFull, chr(34), """")
    													strFull = replace(strFull, chr(39), "'")
    													strFull = replace(strFull, chr(180), "'")
    													strFull = replace(strFull, chr(45), "-")
    													strFull = replace(strFull, chr(63), "&nbsp;")
    												thisfile.write (server.htmlencode(strFull))
    												thisfile.close
    												Set thisfile = Nothing
    												Set fs = Nothing
    		end if
    		rsLink.Close
    Next
    else
    	response.Write "<h2>No Root Found in Rss File </h2>"
    	exit sub 
    End If
    Set objRoot = Nothing
    Set objItems = Nothing
    End If
    
    Set objXML = Nothing 
    end sub 
    
    %>
    
    Code (markup):
    i dont have the time to clear it out but i am sure if you are in ASP you will figure it out how to work with it
     
    ludwig, Mar 14, 2009 IP