Results 1 to 15 of 15

Thread: [RESOLVED] xpath expression help

  1. #1

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Resolved [RESOLVED] xpath expression help

    for each "<entry>" in this xml, i need to get the "title" and the first thumbnail image. that is the first image in the media:group

    Code:
    <?xml version='1.0' encoding='UTF-8'?>
    <feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/'>
    <entry>
    <title>Progression! 9 more pounds to goo!</title>
    <media:group>
    <media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/default.jpg'/>
    <media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/mqdefault.jpg'/>
    <media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/hqdefault.jpg'/>
    <media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/1.jpg'/>
    <media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/2.jpg'/>
    <media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/3.jpg'/>
    </media:group>
    </entry>
    <entry>
    <title>Plank Variations Workout with Max Wettstein</title>
    <media:group>
    <media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/default.jpg'/>
    <media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/mqdefault.jpg'/>
    <media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/hqdefault.jpg'/>
    <media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/1.jpg'/>
    <media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/2.jpg'/>
    <media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/3.jpg'/>
    </media:group>
    </entry>
    </feed>
    this is my code

    vb Code:
    1. Dim xmlDoc As MSXML2.DOMDocument30
    2.     Dim xmlEntryNode As MSXML2.IXMLDOMNode
    3.     Dim xmlEntryNodes As IXMLDOMNodeList
    4.     Dim xmlC1Nodes As IXMLDOMNodeList
    5.     Dim ns As String
    6.     Set xmlDoc = New DOMDocument30
    7.     ns = txtNS.Text
    8.     xmlDoc.setProperty "SelectionLanguage", "XPath"
    9.     xmlDoc.setProperty "SelectionNamespaces", ns
    10.  
    11.     If xmlDoc.loadXML(txtXml.Text) = False Then
    12.         appendText "xml document load failed"
    13.         Exit Sub
    14.     End If
    15.     Set xmlEntryNodes = xmlDoc.documentElement.selectNodes(/x:feed/x:entry)
    16.     Dim i As Integer
    17.     For i = 0 To xmlEntryNodes.length - 1
    18.         Set xmlEntryNode = xmlEntryNodes(i)
    19.         appendText xmlEntryNode.Text
    20.         Set xmlC1Nodes = xmlDoc.selectNodes(//media:group/media:thumbnail[1]/@url)
    21.         If xmlC1Nodes.length > 0 Then
    22.             Dim j As Integer
    23.             For j = 0 To xmlC1Nodes.length - 1
    24.                 appendText xmlC1Nodes(j).Text
    25.             Next
    26.         End If
    27.     Next
    28.  
    29.     Exit Sub

    and here is my output

    Code:
    	
    Progression! 9 more pounds to goo!	
    http://i.ytimg.com/vi/7MTjYXt3rLQ/default.jpg	
    http://i.ytimg.com/vi/O1Nd8lZFGpc/default.jpg	
    Plank Variations Workout with Max Wettstein	
    http://i.ytimg.com/vi/7MTjYXt3rLQ/default.jpg	
    http://i.ytimg.com/vi/O1Nd8lZFGpc/default.jpg
    can anyone pls help me adjust to make it return only one image for each "<entry>"
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  2. #2
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: xpath expression help

    looks like its in your last for j = ... next loop

    replace
    Code:
    Dim j As Integer
      For j = 0 To xmlC1Nodes.length - 1
        appendText xmlC1Nodes(j).Text
      Next
    to

    Code:
    appendtext xmlC1Nodes(0).Text
    if that one is wrong change 0 to 1

    i have not tested any code

  3. #3

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: xpath expression help

    i have tried that, it gives the same image for both entries

    Progression! 9 more pounds to goo!
    http://i.ytimg.com/vi/O1Nd8lZFGpc/default.jpg
    Plank Variations Workout with Max Wettstein
    http://i.ytimg.com/vi/O1Nd8lZFGpc/default.jpg
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  4. #4
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: xpath expression help

    ok maybe this would do it not tested again

    Code:
            Dim i As Integer
              Set xmlC1Nodes = xmlDoc.selectNodes("//media:group/media:thumbnail[1]/@url")
                For i = 0 To xmlEntryNodes.length - 1
                  Set xmlEntryNode = xmlEntryNodes(i)
                  appendText xmlEntryNode.Text
                  appendText xmlC1Nodes(i).Text
              Next

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: xpath expression help

    There's something not quite right here:

    1. The Set statements
    Code:
    Set xmlEntryNodes = xmlDoc.documentElement.selectNodes(/x:feed/x:entry)
    Set xmlC1Nodes = xmlDoc.selectNodes(//media:group/media:thumbnail[1]/@url)
    cause a compile error "Expected Expression"
    I think you need some quotes round the expression in brackets

    2. You're saying that you're getting output but there are 6 <media:thumbnail> elements in each group but you're only reporting 2

    Did you post the actual code and data ?

  6. #6

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: xpath expression help

    yes that was a typo error
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  7. #7
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: xpath expression help

    What other typos are there?

    It doesn't seem to like ("/x:feed/x:entry") either.

    Wouldn't it be easier to copy and paste the actual code that's running ? At least then we have a fighting chance.

    BTW what does the 'appendText' routine do ?

  8. #8
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: xpath expression help

    From what he posted looks like its adds text & vbnewline to textbox/string or adds item to a listbox

    I agree to posting actual code not just a "kind of" like code example

  9. #9

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: xpath expression help

    Hello, here is the attached project

    Name:  xpath_query.png
Views: 129
Size:  18.9 KB
    RssXpath.zip
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  10. #10
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: xpath expression help

    Your problem is that you're changing xmlEntryNodes within a loop controled by xmlEntryNodes.Length making the For / Next loop iterate too many times. EDIT: In fact by the second iteration, it's not the same object that you started with.
    Modified code:
    Code:
    Private Sub cmd1_Click()
        On Error GoTo ErrorHandler:
        txtOut.Text = ""
        'Prepare a regular expression object
        Dim xmlDoc As MSXML2.DOMDocument30
        Dim xmlEntryNode As MSXML2.IXMLDOMNode
        Dim xmlEntryNodes As IXMLDOMNodeList
        Dim xmlEntryNodes1 As IXMLDOMNodeList
        Dim xmlC1Nodes As IXMLDOMNodeList
        Dim ns As String
        Set xmlDoc = New DOMDocument30
        ns = txtNS.Text
        xmlDoc.setProperty "SelectionLanguage", "XPath"
        xmlDoc.setProperty "SelectionNamespaces", ns
    
        If xmlDoc.loadXML(txtXml.Text) = False Then
            appendText "xml document load failed"
            Exit Sub
        End If
        Set xmlEntryNodes = xmlDoc.documentElement.selectNodes(txtXP.Text)
        appendText "ENodes = ", xmlEntryNodes.length
        Dim i As Integer
        For i = 0 To (xmlEntryNodes.length) - 1
            Set xmlEntryNode = xmlEntryNodes(i)
            If xmlDoc.loadXML(xmlEntryNode.xml) = True Then
                Set xmlEntryNodes1 = xmlDoc.documentElement.selectNodes(txtXP2.Text)
                'Set xmlEntryNodes = xmlEntryNode.selectNodes(txtXP2.Text)
                'If xmlC1Nodes.length > 0 Then
                appendText xmlEntryNodes1(0).Text
    
                'End If
    
    
                '            Dim j As Integer
                '            For j = 0 To xmlC1Nodes.length - 1
                '                appendText xmlC1Nodes(j).Text
                '            Next
            End If
            'appendText xmlEntryNode.text
            'Set xmlThumbnailNodes = xmlEntryNode.selectNodes("/media:group/media:thumbnail")
            'Trace "Thumbnail = ", xmlThumbnailNodes.Length
        Next
    
        Exit Sub
    
    ErrorHandler:
        appendText "Error in ParseSearchresult ", Err.Description, Erl
    
    End Sub
    Last edited by Doogle; Sep 2nd, 2012 at 01:14 AM.

  11. #11

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: xpath expression help

    @Doogle thanks, how can i give a rating!
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  12. #12
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: xpath expression help

    There's a little 'star' icon next to the 'report post' icon on the bottom left hand side of the Post, on the same line as 'Reply' and 'Reply With Quote'

    It was much more obvious before the changes

  13. #13

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: [RESOLVED] xpath expression help

    thanks very much, i have been doing this 3 days
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

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

    Re: [RESOLVED] xpath expression help

    I know it is settled but this is the method I would have used
    Code:
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim objDoc As MSXML2.DOMDocument
    Dim objNodeList As IXMLDOMNodeList
    Dim objEntryNode As IXMLDOMNode
    Dim objUrlAttrib As IXMLDOMAttribute
    Dim objMediaNode As IXMLDOMNode
    
        Set objDoc = New MSXML2.DOMDocument
        objDoc.loadXML GetXML ' <-- Load however you need to
        
        ' load all the entry nodes
        Set objNodeList = objDoc.selectNodes("//entry")
        
        For Each objEntryNode In objNodeList
            ' display the title of the current entry node
            Debug.Print objEntryNode.selectSingleNode("title").Text
            
            ' display the first thumbnail url
            Set objMediaNode = objEntryNode.selectSingleNode("media:group/media:thumbnail")
            Set objUrlAttrib = objMediaNode.Attributes.getNamedItem("url")
    
            Debug.Print vbTab & objUrlAttrib.Text
        Next
        
        Set objDoc = Nothing
    End Sub
    
    Private Function GetXML() As String
    Dim strXML(24) As String
    
        strXML(0) = "<?xml version='1.0' encoding='UTF-8'?>"
        strXML(1) = "<feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/'>"
        strXML(2) = "<entry>"
        strXML(3) = "<title>Progression! 9 more pounds to goo!</title>"
        strXML(4) = "<media:group>"
        strXML(5) = "<media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/default.jpg'/>"
        strXML(6) = "<media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/mqdefault.jpg'/>"
        strXML(7) = "<media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/hqdefault.jpg'/>"
        strXML(8) = "<media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/1.jpg'/>"
        strXML(9) = "<media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/2.jpg'/>"
        strXML(10) = "<media:thumbnail url='http://i.ytimg.com/vi/7MTjYXt3rLQ/3.jpg'/>"
        strXML(11) = "</media:group>"
        strXML(12) = "</entry>"
        strXML(13) = "<entry>"
        strXML(14) = "<title>Plank Variations Workout with Max Wettstein</title>"
        strXML(15) = "<media:group>"
        strXML(16) = "<media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/default.jpg'/>"
        strXML(17) = "<media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/mqdefault.jpg'/>"
        strXML(18) = "<media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/hqdefault.jpg'/>"
        strXML(19) = "<media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/1.jpg'/>"
        strXML(20) = "<media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/2.jpg'/>"
        strXML(21) = "<media:thumbnail url='http://i.ytimg.com/vi/O1Nd8lZFGpc/3.jpg'/>"
        strXML(22) = "</media:group>"
        strXML(23) = "</entry>"
        strXML(24) = "</feed>"
        
        GetXML = Join(strXML, vbCrLf)
    End Function

  15. #15

    Thread Starter
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: [RESOLVED] xpath expression help

    thanks for this. i tested and it worked! so no need for all the namespace reference?
    Last edited by coolcurrent4u; Sep 2nd, 2012 at 10:41 AM.
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

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