Source language: Translate to:

XML parser - no plugins - only NB actions

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

Moderator: Neosoft Support

XML parser - no plugins - only NB actions

Postby dpayer » Fri Feb 24, 2012 2:23 pm

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

Postby Tony Kroos » Fri Feb 24, 2012 9:37 pm

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

Postby dpayer » Sat Feb 25, 2012 7:42 am

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

Postby Tony Kroos » Sat Feb 25, 2012 9:13 am

Try to implement some kind of iteration cycle so your project would be able to handle tags of any nesting level
Tony Kroos
 
Posts: 402
Joined: Thu Oct 15, 2009 3:43 pm

Postby Luiz Alfredo » Sat Feb 25, 2012 11:21 am

Thanks David. Very good colaboration.
L.A.G.M.
Luiz Alfredo
 
Posts: 195
Joined: Thu Apr 19, 2007 6:58 am
Location: Brazil

Postby Harakiri » Sun Feb 26, 2012 1:43 pm

Totally great, just what i was looking for... thanks!
User avatar
Harakiri
 
Posts: 75
Joined: Fri Aug 13, 2010 2:07 pm
Location: Mars

Re:

Postby dpayer » Fri Dec 28, 2012 12:10 pm

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

Re: XML parser - no plugins - only NB actions

Postby Tony Kroos » Fri Dec 28, 2012 11:09 pm

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

Re: XML parser - no plugins - only NB actions

Postby dpayer » Sat Dec 29, 2012 9:53 am

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


Return to Sample NeoBook Pubs

Who is online

Users browsing this forum: No registered users and 3 guests

cron