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

LVL 10
ClifAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.