Page 1 of 1

Type mismatch: publication.NBsetvar

PostPosted: Thu May 19, 2011 9:34 am
by dpayer
I am attempting to parse XML using a VBScript to get the root element.

Code: Select all
DIM XMLstring
XMLstring = publication.nbGetVar( "[XMLstring]" )

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.loadXML(XMLString)

Set Root = objXMLDoc.documentElement
publication.nbSetVar "[Rootnode]", Root


I load the XML from file and enter its content to a variable: [XMLstring]

I run the function (above) but get the error: Type mismatch: publication.NBsetvar
Image

Thinking it was a data type error, I defined [Rootnode} to be a string, but that did not help.

Can anyone see the error of this format?

David P

PostPosted: Thu May 19, 2011 10:28 am
by Neosoft Support
I think the error means that the contents of "root" are not compatible with the nbSetVar procedure. Try changing the last line to:

publication.nbSetVar "[Rootnode]", Root.text

PostPosted: Thu May 19, 2011 10:47 am
by dpayer
Neosoft Support wrote:I think the error means that the contents of "root" are not compatible with the nbSetVar procedure. Try changing the last line to:

publication.nbSetVar "[Rootnode]", Root.text


Thanks. (Root.text) seems to work.

Now my bigger problem is that it doesn't generate what I was expecting! It shows all elements, not just the root node.

Still, it is all instructive to me. I am wavering between attempting to let VBScript do some of the heavy lifting (which it seems built to do) versus manually going through w/ NB and interpreting each element and determine the level it is on.

The XMLs I am working with have several child elements with varied levels of further child elements, some with attributes, some not. The scripts I have found thus far haven't given me the granularity I want. But, Im not a VB programmer so my limits surface quickly. :)

Thanks for your help.

PostPosted: Thu May 19, 2011 11:16 am
by Tony Kroos
Can you provide a link or sample of your xml?

PostPosted: Thu May 19, 2011 3:16 pm
by Fabien
Hi,

Why not:

Code: Select all
publication.nbSetVar "[Rootnode]", objXMLDoc.documentElement

Fabien

PostPosted: Thu May 19, 2011 7:43 pm
by Gaev
David P:
The scripts I have found thus far haven't given me the granularity I want. But, Im not a VB programmer so my limits surface quickly.

You don't need to be a VB Programmer ... just have an understanding of accessing the elements within an XML Object ... this page ... http://www.w3schools.com/xml/xml_applications.asp will give you a very good start ... see the section called XML JavaScript in the left panel ... and then the XML Examples.

PostPosted: Fri May 20, 2011 7:45 am
by dpayer
Fabien wrote:Hi,

Why not:

Code: Select all
publication.nbSetVar "[Rootnode]", objXMLDoc.documentElement

Fabien


This gives a data mismatch. But if you use:

publication.nbSetVar "[Rootnode]", objXMLDoc.documentElement.nodename

it will give the root node name as expected.

D

PostPosted: Fri May 20, 2011 9:30 am
by dpayer
Tony Kroos wrote:Can you provide a link or sample of your xml?


I am actually using several sample XMLs to be sure what I am doing will work consistently. I have downloaded the examples at the w3schools.com site (VERY HELPFUL TO UNDERSTAND XML - particularly as you get into the XML DOM sections) and am using some XML from work products here where I am employed.

Here is a helpful starting point, if you are interested in getting the XML to a point of being evaluated. Take a XML string and make the variable [XMLstring]. You can get this string into the VB script using this function named: Root_AllChildren):

Code: Select all
DIM XMLstring
XMLstring = publication.nbGetVar( "[XMLstring]" )

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.loadXML(XMLString)

Set n = objXMLDoc.documentElement.firstchild
publication.nbSetVar "[firstchildnode]", n.nodename
Set p_node = n.parentNode
publication.nbSetVar "[Rootnode]", p_node.nodeName

publication.nbSetVar "[child1nodes_qty]", objXMLDoc.documentElement.childnodes.length


Dim objChildNodes, strNode
Set objChildNodes = objXMLDoc.documentElement.childNodes
DIM list1

For Each strNode In objChildNodes
list1 = (list1 & chr(13) & strNode.nodeName)

Next
publication.nbSetVar "[Child1Nodes]", list1


Then call the function and use this NB code to display what it shows:
Code: Select all
Call "Root_AllChildren"

AlertBox "Root Element" "Root node is: [rootnode]"
Alertbox "Test" "The root has [child1nodes_qty] child nodes"
AlertBox "Childnodes" "List of childnodes: [child1nodes]"


I have found that I can use a browser object as the XML validator. If there is an error in the formatting of the xml, it will throw up an error page and you can get that info using a couple methods).

David P.

PostPosted: Fri May 20, 2011 12:25 pm
by Tony Kroos
dpayer wrote:I have found that I can use a browser object as the XML validator.
omg... you can validate xml inside your function, just before parsing. Check this properties:

objXMLDoc.setProperty "ProhibitDTD", False
objXMLDoc.setProperty "ResolveExternals", True
objXMLDoc.validateOnParse = True

After loading check if
objXMLDoc.parseError.errorCode = 0 then xml ok

Error statements placed at
objXMLDoc.parseError.reason
objXMLDoc.parseError.errorCode
objXMLDoc.parseError.line
objXMLDoc.parseError.linepos
objXMLDoc.parseError.srcText

Use Select Case or IF...Else conditions to parse xml or not (depending on the parseError.errorCode)

PostPosted: Fri May 20, 2011 2:01 pm
by dpayer
Tony Kroos wrote:
dpayer wrote:I have found that I can use a browser object as the XML validator.
omg... you can validate xml inside your function, just before parsing. Check this properties:

objXMLDoc.setProperty "ProhibitDTD", False
objXMLDoc.setProperty "ResolveExternals", True
objXMLDoc.validateOnParse = True

After loading check if
objXMLDoc.parseError.errorCode = 0 then xml ok

Error statements placed at
objXMLDoc.parseError.reason
objXMLDoc.parseError.errorCode
objXMLDoc.parseError.line
objXMLDoc.parseError.linepos
objXMLDoc.parseError.srcText

Use Select Case or IF...Else conditions to parse xml or not (depending on the parseError.errorCode)



Thanks. Helpful info.

D

PostPosted: Fri May 20, 2011 2:14 pm
by Tony Kroos
As for parser, you just need an iteration through each node from the root to collect all info. Smth like this:
...
...
If objXMLDoc.parseError.errorCode = 0 then Iteration(objXMLDoc.documentElement)

Sub Iteration(theNode)
...
... get nodes properties
...
... then check child nodes if any:
Dim cnodes, childitem, x
Set cnodes = theNode.ChildNodes
x = cnodes.length
If (x > 0) Then
For Each childitem In cnodes
Iteration(childitem)
Next
End If
End Sub

This will collect info from parent node specified and all child nodes