Source language: Translate to:

Type mismatch: publication.NBsetvar

Questions and information about using VBScript and JavaScript in NeoBook functions

Moderator: Neosoft Support

Type mismatch: publication.NBsetvar

Postby dpayer » Thu May 19, 2011 9:34 am

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
User avatar
dpayer
 
Posts: 1383
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Neosoft Support » Thu May 19, 2011 10:28 am

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
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5603
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA

Postby dpayer » Thu May 19, 2011 10:47 am

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.
User avatar
dpayer
 
Posts: 1383
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Tony Kroos » Thu May 19, 2011 11:16 am

Can you provide a link or sample of your xml?
Tony Kroos
 
Posts: 402
Joined: Thu Oct 15, 2009 3:43 pm

Postby Fabien » Thu May 19, 2011 3:16 pm

Hi,

Why not:

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

Fabien
¡ sǝıʇɹo sǝl suɐp éɯéɯ sɐd ǝssnod
User avatar
Fabien
 
Posts: 137
Joined: Thu Dec 04, 2008 2:31 pm
Location: France - Lyon

Postby Gaev » Thu May 19, 2011 7:43 pm

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.
User avatar
Gaev
 
Posts: 3736
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby dpayer » Fri May 20, 2011 7:45 am

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
User avatar
dpayer
 
Posts: 1383
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby dpayer » Fri May 20, 2011 9:30 am

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.
User avatar
dpayer
 
Posts: 1383
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Tony Kroos » Fri May 20, 2011 12:25 pm

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)
Tony Kroos
 
Posts: 402
Joined: Thu Oct 15, 2009 3:43 pm

Postby dpayer » Fri May 20, 2011 2:01 pm

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
User avatar
dpayer
 
Posts: 1383
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Tony Kroos » Fri May 20, 2011 2:14 pm

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
Tony Kroos
 
Posts: 402
Joined: Thu Oct 15, 2009 3:43 pm


Return to NeoBook Functions - VBScript & JavaScript

Who is online

Users browsing this forum: No registered users and 3 guests