0

`Here I have converted one string to XML:

xmlString  =    
  "   <?xml version='1.0' encoding='UTF-8' standalone='yes'?>" & _
  "   <hub:notifications>"  & _
  "   <hub:notificationId>728dc361-8b4f-4acc-ad2d-9a63125c5114</hub:notificationId>" & _
  "   <hub:notificationId>5b7c6989-ee27-422c-bbed-2f2c36136c5b</hub:notificationId>" & _
  "   <hub:notificationId>67d1fffe-ab3f-43e3-bb03-24926debe2dc</hub:notificationId>" & _
  "   </hub:notifications>"

objXML.LoadXml(xmlString)

set Node = objXML.selectSingleNode("hub:notifications/hub:notificationId")

   i = 0
   Count = 0
    For Each Node In objXML.selectNodes("hub:notifications") 
       ReDim Preserve aryNotificationIDs (i + 1)
       aryNotificationIDs(i) = Node.selectSingleNode("hub:notificationId").text 
       Count++        
    Next 
  Response.write Count

In above, I am not getting Count of Child nodes and How to get the child node values.

Can any one help me?

Thanks, Jagadi`

Jagadeesh
  • 1,630
  • 8
  • 24
  • 35

1 Answers1

4

There are many problems with your posted code.

First

What language are you using? There seems to be styles from VBScript and JScript. It is predominately VBScript so I'm going to assume that is what you meant to use throughout.

Second

The XML declaration needs to be the first characters in the string. That is:

"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" & _

not

" <?xml version='1.0' encoding='UTF-8' standalone='yes'?>" & _

Third

XML with namespaces requires an xml namespace declaration in the top-level node that use the namespace. For example the root node.

<hub:notifications>

would become

<hub:notifications xmlns:hub='http://stackoverflow.com'>

But you would replace the stackoverflow URL with one appropriate to you.

Fourth

If you want to iterate through the child nodes of hub:notifications then you need to change the FOR deceleration to:

For Each Node In objXML.selectSingleNode("hub:notifications").childNodes

Fifth

i is not increasing in your loop, so you are setting aryNotificationIDs(1) to the different values of the nodes.

Sixth

Related to the first. There is no ++ operator in VBScript. And you don't need both i and Count in the For loop.

Additionally

You don't need to cycle through the nodes to get a count. You can use an xpath selector, and the length property. E.g. objXML.selectNodes("hub:notifications/hub:notificationId").length

Finally

I have taken you code and applied the above suggestions, I've also included an error checking part that checks that the xml has correctly loaded. The code below will output the count of hub:notificationId nodes, and list all the values in the array aryNotificationIDs. I've removed other superfluous code.

xmlString  =  "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" & _
  "   <hub:notifications xmlns:hub='http://stackoverflow.com'>"  & _
  "   <hub:notificationId>728dc361-8b4f-4acc-ad2d-9a63125c5114</hub:notificationId>" & _
  "   <hub:notificationId>5b7c6989-ee27-422c-bbed-2f2c36136c5b</hub:notificationId>" & _
  "   <hub:notificationId>67d1fffe-ab3f-43e3-bb03-24926debe2dc</hub:notificationId>" & _
  "   </hub:notifications>"

Set objXML = Server.CreateObject("Msxml2.DOMDocument")
objXML.LoadXml(xmlString)
If objXML.parseError.errorCode <> 0 Then
    Response.Write "<p>Parse Error Reason: " & objXML.parseError.reason & "</p>"
Else
    For Each node In objXML.selectSingleNode("hub:notifications").childNodes
        ReDim Preserve aryNotificationIDs(i)
        aryNotificationIDs(i) = node.text
        i = i + 1
    Next
    Response.Write "<p>Count: " & i & "</p>"
    For j = 0 to i - 1
        Response.Write "<p>aryNotificationIDs(" & j & ") = " & aryNotificationIDs(j) & "</p>"
    Next
End If
Community
  • 1
  • 1
Chris Kent
  • 862
  • 11
  • 18
  • Thanks for helping. and at 'ReDim Preserve aryNotificationIDs(i). I have removed the Preserve. Due to Type mismatch: 'aryNotificationIDs'. – Jagadeesh Dec 01 '11 at 14:47