ASP and Looping Thru a XML Document

Overthere
Overthere used Ask the Experts™
on
I have a xml document that I wish to loop thru and extract data. The page is working fine but I think it could be coded much efficiently.
In my original coding vIndex was increment and I had a loop etc. It's just messy.
I have posted my new coding and old coding.
The new coding gives me o  from objNodeList.length statement.
What am I doing wrong??
new coding below
	set objNodeList = xdDoc.getElementsByTagName("SERVICEPROVIDER")	
	response.write "<BR>" & objNodeList.length
	if xdDoc.parseError = 0 then
	  if objNodeList.length > 0 then
	     for each x in objNodeList
		     response.write "<BR> inside for/next"
	             vprovider = x.getattribute("SERVICEPROVIDERNAME")
                     vbegdate = x.getattribute("STARTDATE")
                    venddate = x.getattribute("ENDDATE")
	    	     response.write "<BR>Provider is: " & vprovider & "   Startdate: " & vbegdate & "  Ending Date: " & venddate               
		next
	   end if
	 else
        ' display error message
    end if 	 

Open in new window


And this is old coding and was how I was doing looping but messy - incrementing vIndex
               wrkname = SERVICECARD/SERVICEINFO/SERVICEPROVIDERLIST/SERVICEPROVIDER[" & vIndex & "]/SERVICEPROVIDERNAME"
	      wrkstartdate = xdDoc.selectSingleNode("SERVICECARD/SERVICEINFO/SERVICEPROVIDERLIST/SERVICEPROVIDER[" & vIndex & "]/STARTDATE").text
             wrkenddate=  xdDoc.selectSingleNode("SERVICECARD/SERVICEINFO/SERVICEPROVIDERLIST/SERVICEPROVIDER[" & vIndex & "]/ENDDATE").text

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I solved it: this is what my coding looks like now:
Set colNodes = xdDoc.selectNodes ("//SERVICEPROVIDER")
      
      if xdDoc.parseError = 0 then
           for each objSite in colNodes
                 response.write "<BR> inside for/next"
                   vsegnbr = objSite.selectSingleNode("PROVIDERTYPE").text
                      vprovider = objSite.selectSingleNode("SERVICEPROVIDERNAME").text
                    vbegindate = objSite.selectSingleNode("STARTDATE").text
                    venddate = objSite.selectSingleNode("ENDDATE").text
                    response.write "<BR>Provider Type is: " & vsegnbr
                  response.write "<BR>Provider is: " & vprovider
                  response.write "<BR>Begin date is: " & vbegindate
                  response.write "<BR>End date is: " & venddate
            next
       else
        ' display error message
    end if       
        
      response.end
Senior Developer
Commented:
Why looping at all? Just emit the XML:

E.g.
<!-- saved from url=(0016)http://localhost -->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Test</title>
        <style>
            SERVICEPROVIDER             { display: block; }
            SERVICEPROVIDERNAME         { display: block; }
            PROVIDERTYPE                { display: block; }
            STARTDATE                   { display: block; }
            ENDDATE                     { display: block; }
            PROVIDERTYPE::before        { content: "Provider Type is: "; }
            SERVICEPROVIDERNAME::before { content: "Provider is:"; }
            STARTDATE::before           { content: "Begin date is: "; }
            ENDDATE::before             { content: "End date is: "; }
        </style>
    </head>
    <body>
        <SERVICEPROVIDER>
            <PROVIDERTYPE>1</PROVIDERTYPE>
            <SERVICEPROVIDERNAME>2</SERVICEPROVIDERNAME>
            <STARTDATE>3</STARTDATE>
            <ENDDATE>4</ENDDATE>
        </SERVICEPROVIDER>
        <SERVICEPROVIDER>
            <PROVIDERTYPE>5</PROVIDERTYPE>
            <SERVICEPROVIDERNAME>2</SERVICEPROVIDERNAME>
            <STARTDATE>7</STARTDATE>
            <ENDDATE>8</ENDDATE>
        </SERVICEPROVIDER>
    </body>
</html>

Open in new window

Author

Commented:
Thank you for responding. There is much more that goes on behind the scenes than what was posted. Data is extracted, validated and populated into tables and etc.....

Author

Commented:
Thank you - its a good piece of coding to display values. Wasn't quite what I need (I resolved the issue myself).  But I appreciate you sharing and responding.  :}

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial