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

SelectSingleNode fails with MS XML 4.

Hi!

Currently, I am experiencing some problems with selecting nodes.
I am invoking the XML parser using:

Set objXMLParser = CreateObject("Msxml2.DOMDocument.4.0")

objXMLParser.ASync = False
objXMLParser.PreserveWhiteSpace = False
objXMLParser.ResolveExternals = False
objXMLParser.ValidateOnParse = True



Then, later in the code, I use:

Set objXMLChild = Nothing
Set objXMLChild = objXMLItems.ChildNodes.Item(lngCounter)
           
If Not objXMLChild Is Nothing Then
  Select Case objXMLChild.NodeName
    Case "TotalResults"
      lngTotalNumberOfResults = CLng(objXMLChild.Text)
    Case "Item"

      Set objXMLSubChild = Nothing
      Set objXMLSubChild = objXMLChild.SelectSingleNode("LargeImage")

      If Not objXMLSubChild Is Nothing Then
        Set objXMLLargeImageURL = Nothing
        Set objXMLLargeImageURL = objXMLSubChild.SelectSingleNode("URL")
        If Not (objXMLLargeImageURL Is Nothing) Then
          astrLargeImageURL(1) = objXMLLargeImageURL.Text
        End If
      End If
  End Select
End If



However,

If Not objXMLSubChild Is Nothing Then

always returns false.

If I change the initialization part from

Set objXMLParser = CreateObject("Msxml2.DOMDocument.4.0")

to

Set objXMLParser = CreateObject("MSXML.DOMDocument")

everything works as expected. Any idea what could be wrong?

Regards,
Sebastian
0
Sebastian_Mares
Asked:
Sebastian_Mares
  • 3
  • 3
1 Solution
 
Sebastian_MaresAuthor Commented:
If you need a demo, http://maresweb.org/xml.zip .

When you launch the program as-is, it will tell you that it wasn't able to find any images. If you change the XML initialization to MSXML.DOMDocument, it will download Cover.jpg to the application's folder. The SelectSingleNode part returns True when running XML 2 (or whatever MSXML.DOMDocument is) and False when XML 4 or 3 is used.
0
 
Carl TawnSystems and Integration DeveloperCommented:
I think its something to do with the namespace.

If you change then line:

    Set objXMLSubChild = objXMLChild.SelectSingleNode("LargeImage")

To:

    Set objXMLSubChild = objXMLChild.SelectSingleNode("*[local-name()='LargeImage']")

Then it should work OK.
0
 
Carl TawnSystems and Integration DeveloperCommented:
It appears to be due to the default namespace on the document.
If you setup an alias for the default namespace, then you should be able to use that for selecting nodes.

Add the line:
 
        objXMLParser.setProperty "SelectionNamespaces", "xmlns:x='http://webservices.amazon.com/AWSECommerceService/2004-11-10'"

After the line:

        objXMLParser.validateOnParse = True

Then change:

        Set objXMLSubChild = objXMLChild.SelectSingleNode("LargeImage")

To:

       Set objXMLSubChild = objXMLChild.SelectSingleNode("x:LargeImage")


This should then allow you to select the nodes properly using the 'x:' prefix.

Hope this helps.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Sebastian_MaresAuthor Commented:
0
 
Sebastian_MaresAuthor Commented:
Do you know if the URL changed?
0
 
Carl TawnSystems and Integration DeveloperCommented:
http://webservices.amazon.com/AWSECommerceService/2004-11-10  <--  This isn't necessarily a valid URL, its simply a namespace.  A namespace is a way of uniquely naming elements from a specific supplier in order to avoid naming conflicts.  A namespace can be anything you want, just so long as it is unique.  A lot of developers will choose a namespace that incorporates their company URL so that it will be unique.

Hope this clears things up a bit. Namespaces are a bit odd until you get used to them.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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