Reputation: 1800
`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`
Upvotes: 0
Views: 9119
Reputation: 871
There are many problems with your posted code.
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'?>" & _
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.
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
i
is not increasing in your loop, so you are setting aryNotificationIDs(1)
to the different values of the nodes.
Related to the first. There is no ++
operator in VBScript. And you don't need both i
and Count
in the For loop.
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
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
Upvotes: 4