Results 1 to 20 of 20

Thread: How to populate listview with remote xml data?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow How to populate listview with remote xml data?

    Hi all. i have the following remote rss xml in rss.php I want to populate the xml data to vb6 listview. could any one show me how this can be done.Thanks

    Note:The number of items in xml is dynamic not fixedrss.php
    PHP Code:
      <playlist>
      <
    song>
      <
    artist>artistname1</artist
      <
    name>artistname1</name
      <
    image>image1.gif</image
      <
    rating>2028574083</rating
      <
    songid>566</songid
      <
    totalvotes>09898</totalvotes
      </
    song>
        
      <
    song>
      <
    artist>artistname2</artist
      <
    name>artistname2</name
      <
    image>image2.gif</image
      <
    rating>2028574083</rating
      <
    songid>566</songid
      <
    totalvotes>09898</totalvotes
      </
    song>
    ...
      </
    playlist

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: How to populate listview with remote xml data?

    This works.

    I wrote it real fast though, hope it's what you were looking for.

    Run the project and click 'Go'.

    I put the code into a couple subs, so you can easily just put them and use them in your project.

    There's probably an XML library or control you could reference that would be the 'proper' way to do it but I'm not sure so I wrote it with normal VB functions.
    Attached Files Attached Files

  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: How to populate listview with remote xml data?

    Give this a try

    Set a reference to the "Microsoft XML, v3.0" or what ever version you have listed.

    Code:
    Private Sub Form_Load()
    Dim objDoc As MSXML2.DOMDocument
    Dim objNodelist As IXMLDOMNodeList
    Dim objNode As IXMLDOMNode
    Dim lvwItem As ListItem
    
        'load the xml document
        Set objDoc = New MSXML2.DOMDocument
        objDoc.async = False
        objDoc.Load "c:\playlist.xml"
        
        'add all the song nodes into a  nodelist
        Set objNodelist = objDoc.selectNodes("//song")
        
        'Set up the listview
        ListView1.View = lvwReport
        ListView1.ColumnHeaders.Add , , "Artist"
        ListView1.ColumnHeaders.Add , , "Name"
        ListView1.ColumnHeaders.Add , , "Image"
        ListView1.ColumnHeaders.Add , , "Rating"
        ListView1.ColumnHeaders.Add , , "Song ID"
        ListView1.ColumnHeaders.Add , , "Total Votes"
        
        'Loop through each song node and add to the list view
        For Each objNode In objNodelist
            Set lvwItem = ListView1.ListItems.Add(, , objNode.selectSingleNode("artist").Text)
            lvwItem.SubItems(1) = objNode.selectSingleNode("name").Text
            lvwItem.SubItems(2) = objNode.selectSingleNode("image").Text
            lvwItem.SubItems(3) = objNode.selectSingleNode("rating").Text
            lvwItem.SubItems(4) = objNode.selectSingleNode("songid").Text
            lvwItem.SubItems(5) = objNode.selectSingleNode("totalvotes").Text
        Next objNode
        
        Set lvwItem = Nothing
        Set objNodelist = Nothing
        Set objDoc = Nothing
    End Sub

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to populate listview with remote xml data?

    Thank you for both of you. Could you both tell me if the rss is located on remote server then how should i achive this task? The extention of the rss is php not xml so it might not work in the same way!!

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: How to populate listview with remote xml data?

    Do you have a url you could post?

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: How to populate listview with remote xml data?

    You can use the Inet (Microsoft Internet Transfer Control) to get the source of the PHP page.

    I have a module that doesn't require the inet control but it's been giving me weird results lately so I'm not going to recommend it anymore.

    After adding the inet control to your form you could do something like:

    vb Code:
    1. Dim strRSS As String
    2.  
    3. strRSS = Inet1.OpenURL("http://url.com/rss.php")

    Then feed strRSS into mine or Mark's function. Note that if there is other code in the PHP page besides the RSS data, you'll need to extract the RSS data from it first.

    But the sub I wrote (and I think Mark's too) should ignore any non-important data.

    Mark - Are you the Mark from a1vbcode.com?

  7. #7
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: How to populate listview with remote xml data?

    Quote Originally Posted by DigiRev
    Mark - Are you the Mark from a1vbcode.com?
    That would be me.

    If it is a true rss feed you should be able to just use the url where I plugged in a file name.

    Change this
    Code:
    objDoc.Load "c:\playlist.xml"
    to this
    Code:
    objDoc.Load "http://url.com/rss.php" '<-- The url to your rss feed

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to populate listview with remote xml data?

    Digirev thanks for your reply. I add the code in to onload event but it never loads the remotee xml for me!!! I used massage box to view the data and i saw all the data from remote xml but it never loads it to textbox!!

    1 Code:
    1. Private Sub Form_Load()
    2. Dim Text1 As String
    3. Text1 = Inet1.OpenURL("http://localhost/rss.php")
    4.   MsgBox " " + Text1
    5.     InitListView ListView1
    6. End Sub

    Furthermore, how to make sure my listview always has fresh data? My intention is to load that rss to listview and update it regulerly so i have updated data at all time.I be happy if i get in this regard too.Thanks
    Last edited by tony007; May 3rd, 2007 at 10:04 AM.

  9. #9
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: How to populate listview with remote xml data?

    To clear the items in the ListView, put:

    ListView.ListItems.Clear

    Before calling the other subs that load the RSS into the ListView.

    Also, I wouldn't declare a string with the name 'Text1' especially if you have a textbox on your form named Text1. Just declare it as something unique like:

    Dim strRSS As String

    And when adding one string to another, don't use the + sign, use &

    Also, are you actually loading the RSS from your own computer? (localhost) or is that just an example?

    It'd help to know what URL you're loading the RSS from so we can see it/test it.

  10. #10
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: How to populate listview with remote xml data?

    Another thing, InitListView isn't the code that loads the RSS. That one just sets up the column headers in the ListView.

    I forgot what I named the sub that actually loads the RSS data, RSSToListView or something like that. It should be called after the InitListView sub.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to populate listview with remote xml data?

    I was thinking that i need to feed it to textbox first(that is why i called it Text1) . I fixed that problem. Now the rss data loads in textbox but all the data in one big line and when i press the button nothing happens. I find it strange why all rss data shows in one line!!

    I even used richtext box still didn't work!!

    I also need some how to get fresh data at all time after making it work at start!!

    Note:I am testing localy an example before i put things online.

    1 Code:
    1. Private Sub Form_Load()
    2. 'Dim Text1 As String
    3. Text1 = Inet1.OpenURL("http://localhost/script.php")
    4.  ' MsgBox " " + Text1
    5.     InitListView ListView1
    6. End Sub


    script.php code
    PHP Code:
    <?php
    header
    ("Cache-Control: no-cache, must-revalidate");
        
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    $server   "localhost"// MySQL hostname
    $username "test"// MySQL username
    $password "test"// MySQL password
    $dbname   "test"// MySQL db name

    $db mysql_connect($server$username$password) or die(mysql_error());
          
    mysql_select_db($dbname) or die(mysql_error());

    // this is necessary, otherwise it won't work: 
    header('Content-type: application/xml'); 
    // you need to return the error as xml as well 
    $res mysql_query('SELECT * FROM songs ORDER by date') or die('<error>'.mysql_error().'</error>'); 
    // display the root node of the xml, and start looping over the elements: 
    echo '<playlist>'
    while(
    $row mysql_fetch_assoc($res)){ 
      echo 
    '<song>'
      echo 
    '<artist>'.$row['artist'].'</artist>'
      echo 
    '<name>'.$row['name'].'</name>'
      echo 
    '<image>'.$row['image'].'</image>'
      echo 
    '<rating>'.$row['rate'].'</rating>'
      echo 
    '<songid>'.$row['songid'].'</songid>'
      echo 
    '<totalvotes>'.$row['totvotes'].'</totalvotes>'
      echo 
    '</song>'

    echo 
    '</playlist>';  


    ?>
    Last edited by tony007; May 3rd, 2007 at 11:28 AM.

  12. #12
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: How to populate listview with remote xml data?

    It's showing it all on one line because that is how your PHP script is outputting it. There are no line breaks in the PHP code's output (\n).

    Also, don't store it in a TextBox first, it's not necessary. The TextBox is also limited to 64KB of data so if the RSS ever exceeds that then it will mess things up. Just use a string variable and do NOT name it Text1.

    The code I gave you expects new lines after each tag. I assumed they were there because that is how you displayed it in your example.

    Use Mark's code, it should work.

    And change the one line to what he said in post #7.

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to populate listview with remote xml data?

    Quote Originally Posted by MarkT
    That would be me.

    If it is a true rss feed you should be able to just use the url where I plugged in a file name.

    Change this
    Code:
    objDoc.Load "c:\playlist.xml"
    to this
    Code:
    objDoc.Load "http://url.com/rss.php" '<-- The url to your rss feed
    Mark i tried your solution. But i keep getting this error:

    Code:
    Compile error:
    
    User-defined type not defined
    pointing at:

    Dim lvwItem As ListItem

  14. #14
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: How to populate listview with remote xml data?

    Do you have a listview on the page?

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to populate listview with remote xml data?

    Quote Originally Posted by MarkT
    Do you have a listview on the page?
    Mark thanks i added listview it got fixed.Since this rss feed data. Is there away that i make sure this listview has fresh data almost instantly?I have doen similer thing using ajax and javascript but not sure how to achive it in vb6.could you show me how this can done?

  16. #16
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: How to populate listview with remote xml data?

    The easy way to do it would be to place timer on the page and have it reload the data at set intervals.

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to populate listview with remote xml data?

    Quote Originally Posted by MarkT
    The easy way to do it would be to place timer on the page and have it reload the data at set intervals.
    I have this timer o reload webbrowser controle. But don't know how to update listview every few seconds using it.could you let me know how to get updated data for code you provided?Thanks

    1 Code:
    1. Dim secs As Integer
    2.  
    3. Private Sub Form_Load()
    4.  
    5. Timer1.Interval = 60000 '1 second
    6. Timer1.Enabled = True
    7. WebBrowser1.Navigate2 "http://www.somesite.com/page.php"
    8. End Sub
    9.  
    10. Private Sub Timer1_Timer()
    11.  
    12. secs = secs + 1
    13.    
    14.     If secs = 30 Then
    15.     Counter = Counter + 1 'counter for each 30 seconds that pass
    16.     secs = 0 'rest for next 3
    17.     End If
    18.    
    19.     If Counter = 4 Then '2 mins are up
    20.     WebBrowser1.Navigate2 "http://www.somesite.com/page.php"
    21.     Counter = 0 'reset
    22.     End If
    23.    
    24.    
    25. End Sub

  18. #18
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: How to populate listview with remote xml data?

    If I understand you, this should reload the listview every 2 minutes
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        'Set up the listview
        ListView1.View = lvwReport
        ListView1.ColumnHeaders.Add , , "Artist"
        ListView1.ColumnHeaders.Add , , "Name"
        ListView1.ColumnHeaders.Add , , "Image"
        ListView1.ColumnHeaders.Add , , "Rating"
        ListView1.ColumnHeaders.Add , , "Song ID"
        ListView1.ColumnHeaders.Add , , "Total Votes"
        PopulateListview
        
        Timer1.Interval = 60000 ' <-- one minute
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
    Static lngMin As Long
    
        lngMin = lngMin + 1
        
        'every 2nd timer tick reload the listview
        If lngMin Mod 2 Then
            PopulateListview
        End If
    
    End Sub
    
    Private Sub PopulateListview()
    Dim objDoc As MSXML2.DOMDocument
    Dim objNodelist As IXMLDOMNodeList
    Dim objNode As IXMLDOMNode
    Dim lvwItem As ListItem
    
        'load the xml document
        Set objDoc = New MSXML2.DOMDocument
        objDoc.async = False
        objDoc.Load "http://localhost/rss.php"
        
        'add all the song nodes into a  nodelist
        Set objNodelist = objDoc.selectNodes("//song")
        
        'Clear the listview
        ListView1.ListItems.Clear
    
        'Loop through each song node and add to the list view
        For Each objNode In objNodelist
            Set lvwItem = ListView1.ListItems.Add(, , objNode.selectSingleNode("artist").Text)
            lvwItem.SubItems(1) = objNode.selectSingleNode("name").Text
            lvwItem.SubItems(2) = objNode.selectSingleNode("image").Text
            lvwItem.SubItems(3) = objNode.selectSingleNode("rating").Text
            lvwItem.SubItems(4) = objNode.selectSingleNode("songid").Text
            lvwItem.SubItems(5) = objNode.selectSingleNode("totalvotes").Text
        Next objNode
        
        Set lvwItem = Nothing
        Set objNodelist = Nothing
        Set objDoc = Nothing
    End Sub

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to populate listview with remote xml data?

    Mark i realy thank you for this usefull code. It is working.However,

    is there a way to create another listview and populate it with initial data and each time compare the Name columns of both listviews and if they are diffrent update the second listview. I am thinking of some how hiding unessary refreshes since i keep loosing focouse on the form!!could you help me with a solution for this unncessary refreshes?


    Furthermore, In the same project i added another timer that frequently checks a remote xml. What i want this new timer some how check if the rss feed is giving any result/data set out. If it gives data out i need for example reload webbrowser controle.If it doesn't give any data out i do nothing. (I don't need to output xml content)Could you show me how i can make such check using your code.Thanks

    Note:The url to check for its xml data is like this:
    'http://localhost//datastatus.php?sessionkey=b429632c627bcf6bd4840561690e3c49



    1 Code:
    1. Private Sub Form_Load()
    2.     'Set up the listview
    3.     ListView1.View = lvwReport
    4.     ListView1.ColumnHeaders.Add , , "Artist"
    5.     ListView1.ColumnHeaders.Add , , "Name"
    6.     ListView1.ColumnHeaders.Add , , "Image"
    7.     ListView1.ColumnHeaders.Add , , "Rating"
    8.     ListView1.ColumnHeaders.Add , , "Song ID"
    9.     ListView1.ColumnHeaders.Add , , "Total Votes"
    10.     PopulateListview
    11.    
    12.     'Timer1.Interval = 60000 ' <-- one minute
    13.     Timer1.Interval = 7000 ' <-- 10 seconds
    14.     Timer1.Enabled = True
    15.    
    16.      Timer2.Interval = 7000 ' <-- 10 seconds
    17.     Timer2.Enabled = True
    18. End Sub
    19.  
    20. Private Sub Timer2_Timer()
    21. Static lngMin As Long
    22.  
    23.     lngMin = lngMin + 1
    24.    
    25.     'every 2nd timer tick reload the listview
    26.     If lngMin Mod 2 Then
    27.         checkForNewData
    28.     End If
    29.  
    30. End Sub
    31.  
    32. Private Sub checkForNewData()
    33. 'Here i need to check for new data . If new data is avalible then
    34. 'reload the webbrowser.
    35.  
    36. 'i need some how check if the following php code outputs any xml or not?
    37. 'http://localhost//datastatus.php?sessionkey=b429632c627bcf6bd4840561690e3c49
    38.  
    39.  
    40. End Sub

    datastatus.php

    PHP Code:
    <?php
    header
    ("Cache-Control: no-cache, must-revalidate");
        
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    $sessionkey=$_GET['sessionkey'];

    $server   "localhost"// MySQL hostname
    $username "root"// MySQL username
    $password "root"// MySQL password
    $dbname   "db"// MySQL db name

    $db mysql_connect($server$username$password) or die(mysql_error());
          
    mysql_select_db($dbname) or die(mysql_error());

    // this is necessary, otherwise it won't work: 
    header('Content-type: application/xml'); 
    // you need to return the error as xml as well 
    $res mysql_query("SELECT w,h FROM datastatus WHERE who_sessid ='$sessionkey' ") or die('<error>'.mysql_error().'</error>');
    // display the root node of the xml, and start looping over the elements: 
    echo '<playlist>'
    while(
    $row mysql_fetch_assoc($res)){ 
      echo 
    '<song>'
      echo 
    '<artist>'.$row['w'].'</artist>'
      echo 
    '<name>'.$row['h'].'</name>'
      echo 
    '</song>'

    echo 
    '</playlist>';  


    ?>
    Last edited by tony007; May 4th, 2007 at 08:55 PM.

  20. #20
    Frenzied Member Robbo's Avatar
    Join Date
    Jan 2001
    Location
    Bradford
    Posts
    1,143

    Re: How to populate listview with remote xml data?

    is there a way of doing this on powerpoint and having a feed on a slide show?
    -----------------------------------------------
    "The hall is rented,"
    "the orchestra is engaged,"
    "its now time to see if you can dance!"
    Q, Q-Who, Star Trek The Next Generation
    -----------------------------------------------
    General Work day

    -----------------------------------------------
    DOS, Win 95, Win 98 SE, Win ME, Win NT 4.0 SP6a, Windows 2000 SP3, Window XP SP1, Windows 7, Windows 8/8.1, Windows 10, Office 97 Pro, Office 2000 Pro, Office 2010, Office 2013, Office 2016, Office 2019, Visual Basic 6 (SP5), SQL, Oracle

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width