egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

looking for xml2dict

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
CrazyCat
Revered One


Joined: 13 Jan 2002
Posts: 1032
Location: France

PostPosted: Fri Nov 05, 2010 5:06 am    Post subject: looking for xml2dict Reply with quote

Hello there,

I'm currently working on a tcl which use an XML as datasource.
I've found an usefull procedure:
Code:
proc xml2list xml {
     regsub -all {>\s*<} [string trim $xml " \n\t<>"] "\} \{" xml
     set xml [string map {> "\} \{#text \{" < "\}\} \{"}  $xml]

     set res ""   ;# string to collect the result
     set stack {} ;# track open tags
     set rest {}
     foreach item "{$xml}" {
    switch -regexp -- $item {
       ^# {append res "{[lrange $item 0 end]} " ; #text item}
       ^/ {
      regexp {/(.+)} $item -> tagname ;# end tag
      set expected [lindex $stack end]
      if {$tagname!=$expected} {error "$item != $expected"}
      set stack [lrange $stack 0 end-1]
      append res "\}\} "
     }
       /$ { # singleton - start and end in one <> group
          regexp {([^ ]+)( (.+))?/$} $item -> tagname - rest
          set rest [lrange [string map {= " "} $rest] 0 end]
          append res "{$tagname [list $rest] {}} "
       }
       default {
          set tagname [lindex $item 0] ;# start tag
          set rest [lrange [string map {= " "} $item] 1 end]
          lappend stack $tagname
          append res "\{$tagname [list $rest] \{"
       }
    }
    if {[llength $rest]%2} {error "att's not paired: $rest"}
     }
     if [llength $stack] {error "unresolved: $stack"}
     string map {"\} \}" "\}\}"} [lindex $res 0]
 }

Put I prefer working with dict than with list, so does anyone has an xml2dict procedure ?
I think I can do it, but I'm quite lazy Smile

Thanks by advance
_________________
https://www.eggdrop.fr - French IRC network
Offer me a coffee - Do not ask me help in PM, we are a community.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber