• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5540
  • Last Modified:

selectSingleNode

Given the attached XML code, I have retrieved a NodeList.  

Set oNodeList = oXML.documentElement.selectNodes("//book/*")

Now I want to step through the NodeList and select a single Node by name.
For Each oNode In oNodeList
    sAuthorLastName = oNode.selectSingleNode("author/last-name"),Text
Next

But I keep getting "Object variable or With block variable not set" errors.

This does work:
    sAuthorLastName = oNode.childNodes(1).childNodes(1).Text
But is not good coding.

What am I doing wrong?
<?xml version="1.0"?>
<!-- A fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
  <book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-8">
    <title>Pride And Prejudice</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>24.95</price>
  </book>
  <book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>29.95</price>
  </book>
  <book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-6">
    <title>Emma</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" publicationdate="1982" bk:ISBN="1-861001-45-3">
    <title>Sense and Sensibility</title>
    <author>
      <first-name>Jane</first-name>
      <last-name>Austen</last-name>
    </author>
    <price>19.95</price>
  </book>
</bookstore>

Open in new window

0
Clif
Asked:
Clif
  • 4
  • 2
1 Solution
 
PaulHewsCommented:
This line:
Set oNodeList = oXML.documentElement.selectNodes("//book/*")

Selects all the nodes under the book elements.  You actually want to select all the book elements:

Set oNodeList = oXML.documentElement.selectNodes("//book")
MsgBox oNodeList.length
 
'Now I want to step through the NodeList and select a single Node by name.
For Each oNode In oNodeList
    sAuthorLastName = oNode.selectSingleNode("author/last-name").Text
    Debug.Print sAuthorLastName
Next

Open in new window

0
 
ClifAuthor Commented:
That seems to work for the first book, and indeed oNodeList.Length is 4, but it doesn't seem to be picking up the remaining book authors in the subsequent loop iterations.  After the first loop through, I then get the "Object variable..." error for the next three iterations.
0
 
PaulHewsCommented:
Here is the full code I used to test... It generated this output:
Austen
Atwood
Austen
Austen

Just as expected.
Private Sub Command1_Click()
    Dim oXML As DOMDocument40
    Dim oNodeList As IXMLDOMNodeList
    Dim oNode As IXMLDOMNode
    
    Set oXML = New DOMDocument40
    oXML.Load "C:\temp\test.xml"
    
    
    
    Set oNodeList = oXML.documentElement.selectNodes("//book")
    'MsgBox oNodeList.length
 
    'Now I want to step through the NodeList and select a single Node by name.
    For Each oNode In oNodeList
        sAuthorLastName = oNode.selectSingleNode("author/last-name").Text
        Debug.Print sAuthorLastName
    Next
    
   
 
End Sub

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
PaulHewsCommented:
You might check your object declarations.  Maybe you need to use a more recent version of the XML library?  Failing that, if you can post your test code, I'll see if I can replicate the problem here.
0
 
ClifAuthor Commented:
Sorry about that.  It works.

The last issue had to do with the source which was giving me a malformed xml (the sample was just that, not actual xml that I'm using).

Thanks for your help.  :)
0
 
PaulHewsCommented:
Glad you got it sorted.  Thanks.  :)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now