Solved

Parse only unique values from an XML response using XMLDOM...???

Posted on 2006-10-23
6
305 Views
Last Modified: 2008-02-01
I'm parsing an XML response like so:

'open the XML document
      Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
      xmlDoc.Async = false
      xmlDoc.LoadXML(APIResponse)
      
'parse data from response
      Set RecordsNode = xmlDoc.selectNodes("/fmresultset/resultset/record")
            For Each recordNode In RecordsNode
                  EmailAddress = TryNode(recordNode, "field[@name='Leads_Clients::Email_Address']/data")
                  Response.Write(EmailAddress & "<br>")
            Next
      Set RecordsNode = Nothing

It works great, however, many of the email addresses returned in this are for the same person.  (the request grabs all emails for customers who have purchased a particular item from us, and many have purchased multiple similar items.)

I need to parse out only unique values, so basically I need the equivalent of a SQL SELECT DISTINCT.

Is there a way to do that from within XMLDOM?  Any information would be greatly appreciated.  thanks!
0
Comment
Question by:Andrew Angell
  • 4
  • 2
6 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17792255
Probably your best bet is to add the values to recordset you create on the fly and then select unique values only.
0
 
LVL 11

Author Comment

by:Andrew Angell
ID: 17792557
Can you guide me on getting the values into a recordset?  I'm a little confused there since I'm not actually selecting from a DB at that point.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17793313
>> I'm a little confused there since I'm not actually selecting from a DB at that point.<<
You don't have to be.

I suspect there is a better way of skinning this cat, but the following example certainly works:

Const adVarchar = 200

Dim rs
Dim EmailAddress, EmailAddresses

EmailAddresses = Array("myemail@domain.com", "username@domain.com", "anotheruser@domain.com", "myemail@domain.com", "anotheruser@domain.com")         ' Create some email addresses with duplicates

Set rs = CreateObject("ADODB.Recordset")
rs.Fields.Append "Email_Address", adVarchar, 1000
rs.Open

For Each EmailAddress In EmailAddresses
    rs.AddNew
    rs.Fields("Email_Address").Value = EmailAddress
    rs.Update
Next

rs.Sort = "Email_Address"
EmailAddress = ""
Do While Not rs.EOF
    If StrComp(rs.Fields("Email_Address").Value, EmailAddress, vbTextCompare) <> 0 Then
        EmailAddress = rs.Fields("Email_Address").Value
        Debug.Print EmailAddress
    End If
    rs.MoveNext
Loop
rs.Close
Set rs = Nothing
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 17793323
So in your case it would look something like this:

Const adVarchar = 200

Dim rs
Dim EmailAddress

Set rs = CreateObject("ADODB.Recordset")
rs.Fields.Append "Email_Address", adVarchar, 1000
rs.Open

'open the XML document
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = false
xmlDoc.LoadXML(APIResponse)
     
'parse data from response
Set RecordsNode = xmlDoc.selectNodes("/fmresultset/resultset/record")
For Each recordNode In RecordsNode
      rs.AddNew
      EmailAddress = TryNode(recordNode, "field[@name='Leads_Clients::Email_Address']/data")
      rs.Fields("Email_Address").Value = EmailAddress
      rs.Update
Next
Set RecordsNode = Nothing

rs.Sort = "Email_Address"
EmailAddress = ""
Do While Not rs.EOF
      If StrComp(rs.Fields("Email_Address").Value, EmailAddress) <> 0 Then
            EmailAddress = rs.Fields("Email_Address").Value
            Response.Write(EmailAddress & "<br>")
      End If
      rs.MoveNext
Loop
rs.Close
Set rs = Nothing
0
 
LVL 11

Author Comment

by:Andrew Angell
ID: 17793413
Works great and I learned something too.  Thanks!  Would you mind explanining a little bit about the Const adVarchar parts?  I've never used that and don't know what it does.  thanks!
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17795636
>>Would you mind explanining a little bit about the Const adVarchar parts?  I've never used that and don't know what it does.<<
Not at all.  In order to build a recordset on the fly you need to define the data types for each field.  adVarchar is just an ADO constant that defines a (you got it) varchar (variable length string).  See here for a complete list of ADO constants:
http://www.carlprothman.net/Technology/DataTypeMapping/tabid/97/Default.aspx
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question