XML parser - no plugins - only NB actions

Share sample pubs, scripts, etc. with other NeoBook users

Moderator: Neosoft Support

Locked
User avatar
dpayer
Posts: 1394
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

XML parser - no plugins - only NB actions

Post by dpayer »

I am getting ready for a project that will require control of XMLs and I decided to work through my own parser even though I know DEC has a plugin for this.

This sample shows how you can browse for an XML, display its root element and a list of all child elements and values. It allows you to export a section of the XML at the child level. It has no editing functions. It also does not validate the xml ( you would usually do this first but I am working in an environment where the files are already in production).

I parse to the 8th child level but you will see from the NB scripting that this can be easily expanded to whatever level you will be working.

Image

This for me is more of a proof of concept than anything. Feel free to use the ideas for anything you would be using if you think it could be helpful.

It parses somewhat slowly in development mode but when compiled, it is fairly fast.

This is the NB pub file
This is the compiled file
This is a sample XML file you can test with.

Feedback is welcome. I am aware of small issue when a value is clicked.

David P.
Tony Kroos
Posts: 419
Joined: Thu Oct 15, 2009 3:43 pm

Post by Tony Kroos »

omg :shock:
here is a function to parse XML DOM tree (tags with #text only) with any level of nesting.

Code: Select all

On Error resume next
Dim i, xmlDoc
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("[%1]")

Iteration(XMLDoc.documentElement)

Sub Iteration(theNode)
    nodeinfo = theNode.NodeName
    If theNode.NodeName = "#text" Then
        chk=nbGetVar ("["&theNode.parentnode.NodeName&i&"]")
        If Len(chk)>0 OR IsEmpty(i) then i=i+1
        nbsetvar "["&theNode.parentnode.NodeName&i&"]", theNode.nodeValue
    End if
    Dim cnodes, citem, x
    Set cnodes = theNode.ChildNodes
    x = cnodes.length
      If Not ( x < 1 ) Then
        For Each citem In cnodes
            Iteration(citem)
        Next
    End If
End Sub
[%1] - place this parameter for local or url path to xml

Call "XML" "Local or url path to xml"

results placed to tag named arrays.
User avatar
dpayer
Posts: 1394
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Post by dpayer »

Tony Kroos wrote:omg :shock:
here is a function to parse XML DOM tree (tags with #text only) with any level of nesting..
Thanks Tony,

There are times when you seek to gain a mastery of understanding, you start off doing something without the final product someone else made. This was that kind of project for me. In fact, our recent conversation on doing sorting caused me to think through how one would do sorting without the use of other tools in the NB tool set.

I consider my XML project a phase one effort. I plan to revisit it again and I will include such methods as you present.

David P.
Tony Kroos
Posts: 419
Joined: Thu Oct 15, 2009 3:43 pm

Post by Tony Kroos »

Try to implement some kind of iteration cycle so your project would be able to handle tags of any nesting level
Luiz Alfredo
Posts: 198
Joined: Thu Apr 19, 2007 6:58 am
Location: Brazil

Post by Luiz Alfredo »

Thanks David. Very good colaboration.
L.A.G.M.
User avatar
Harakiri
Posts: 75
Joined: Fri Aug 13, 2010 2:07 pm
Location: Mars
Contact:

Post by Harakiri »

Totally great, just what i was looking for... thanks!
User avatar
dpayer
Posts: 1394
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Re:

Post by dpayer »

Tony Kroos wrote:omg :shock:
here is a function to parse XML DOM tree (tags with #text only) with any level of nesting.

Code: Select all

On Error resume next
Dim i, xmlDoc
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("[%1]")

Iteration(XMLDoc.documentElement)

Sub Iteration(theNode)
    nodeinfo = theNode.NodeName
    If theNode.NodeName = "#text" Then
        chk=nbGetVar ("["&theNode.parentnode.NodeName&i&"]")
        If Len(chk)>0 OR IsEmpty(i) then i=i+1
        nbsetvar "["&theNode.parentnode.NodeName&i&"]", theNode.nodeValue
    End if
    Dim cnodes, citem, x
    Set cnodes = theNode.ChildNodes
    x = cnodes.length
      If Not ( x < 1 ) Then
        For Each citem In cnodes
            Iteration(citem)
        Next
    End If
End Sub
[%1] - place this parameter for local or url path to xml

Call "XML" "Local or url path to xml"

results placed to tag named arrays.
Tony, I am picking up my XML parser again to improve it and clarify subroutines and possible formation of functions.

I was able to implement your function but I notice that it does nothing with attributes or comments. It parses and appears to create a variable for each element's name with the with the value of that variable displayed. When I loaded very complex xml into it, it did not see all elements on each level. It stopped after following an extended set of child nodes.

Have you developed this at all? Or what are you using it for?

David P.
David Payer
Des Moines, Iowa
USA
Tony Kroos
Posts: 419
Joined: Thu Oct 15, 2009 3:43 pm

Re: XML parser - no plugins - only NB actions

Post by Tony Kroos »

It was just a very simple example of iteration procedure, if you apply this xml example and expected result I would try to improve it.
User avatar
dpayer
Posts: 1394
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Re: XML parser - no plugins - only NB actions

Post by dpayer »

Tony Kroos wrote:It was just a very simple example of iteration procedure, if you apply this xml example and expected result I would try to improve it.
Thanks.

D
David Payer
Des Moines, Iowa
USA
Locked