Link to home
Start Free TrialLog in
Avatar of Clif
ClifFlag for United States of America

asked on

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

Avatar of PaulHews
PaulHews
Flag of Canada image

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

Avatar of Clif

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of PaulHews
PaulHews
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of Clif

ASKER

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.  :)
Glad you got it sorted.  Thanks.  :)