INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Tcl/XML parser problem

Tcl/XML parser problem

(OP)
Hi.
Can anybody help me resolve the problem.
This is my code:

CODE --> tcl

package require tdom 0.7.5

set xml {<teryt>
  <catalog date="2013-01-01" type="all" name="TERC">
     <row>
       <col name="WOJ">02</col>
       <col name="POW"/>
       <col name="GMI"/>
       <col name="RODZ"/>
       <col name="NAZWA">DOLNOŚLĄSKIE</col>
       <col name="NAZDOD">województwo</col>
       <col name="STAN_NA">2013-01-01</col>
       <col name="OPIS">
         <linia> 
         </linia><linia>Kaucja:  2 000,00 zł
         </linia><linia>Informacje zawarte w opisie mają charakter orientacyjny i nie stanowią oferty handlowej.
         </linia><linia>
         </linia><linia>Oferta wysłana z programu IMO dla biur nieruchomości
         </linia><linia/>
       </col>
     </row>
     <row>
       <col name="WOJ">02</col>
       <col name="POW">01</col>
       <col name="GMI">01</col>
       <col name="RODZ">1</col>
       <col name="NAZWA">Bolesławiec</col>
       <col name="NAZDOD">gmina miejska</col>
       <col name="STAN_NA">2013-01-01</col>
       <col name="OPIS">
         <linia> 
         </linia><linia>Linia 222222222222
         </linia><linia>Liniaaaaaa 3333333333
         </linia><linia>
         </linia><linia/>
       </col>
     </row>
  </catalog>
</teryt> }

set doc [dom parse $xml]
set root [$doc documentElement]
  set teryt [$root selectNodes {/teryt} ]
  set row [$root selectNodes {/teryt/catalog/row/col} ]
  
  set licznik 0
  set licznik1 0
  foreach node $row {
    set name [$node @name]
    set val [$node text]
    ns_log notice "name=$name; val=$val"
    if { [$node @name] == "OPIS" } {
      
      set o_opis [$root selectNodes {/teryt/catalog/row/col/linia/text()}]
      #[$node text] ;#[$root selectNodes {/teryt/catalog/row/col/linia/text()}]
      foreach node $o_opis {
        set attList [$node linia *]
        foreach attribute $attList {
          puts "[$node linia]"
        } 
      }
    }

    incr licznik1
  }

$doc delete 

I need write information from <col name="OPIS"><linia></linia></col> but the output is NULL ;/ anybody help me with this ?

RE: Tcl/XML parser problem

Hi,

Rather than examining your script, I opted for a custom solution. I hope it's what you are looking for:

CODE -->

set i 0
set start 0
foreach line [split $xml \n] {
  if {[string first "col name=\"OPIS\"" $line]>0} {incr start}
  if {([string trim  $line]=="<linia>") && $start} {incr start}
  if {[string first "/col" $line]>0} {set start 0}
  if {$start>1} {
    set z [string last ">" $line]
    puts [string range $line [expr $z+1] end]
  }
  incr i
} 

It outputs:

CODE -->

Kaucja:  2 000,00 z?
Informacje zawarte w opisie maj? charakter orientacyjny i nie stanowi? oferty handlowej.

Oferta wys?ana z programu IMO dla biur nieruchomo?ci

 
Linia 222222222222
Liniaaaaaa 3333333333 

I am sure that it is also possible with regular expressions, I will leave that to another enthusiast.
thacoda

RE: Tcl/XML parser problem

(OP)
This is not exacly what I looking for. I found a solution but they give me too much data ;/

CODE --> tcl

(..)
if { [$node @name] == "OPIS" } {
  foreach n [$root selectNodes {//col[@name='OPIS']/linia}] { 
    puts [$n asXML]
  }
}
(..) 
The value from <col name="OPIS"> is split from all <row>. I need draw value from <col name="OPIS"> separately, now puts [$node asXML] give me all data two times ;/
Any idea how to resolve this ??

RE: Tcl/XML parser problem

(OP)
OK, I found a solution ;)
This is the right code:

CODE

set doc [dom parse $xml]
set root [$doc documentElement]
  set teryt [$root selectNodes {/teryt} ]
  set row [$root selectNodes /teryt/catalog/row ]
  
  set licznik 0
  set licznik_row 0
  
  foreach node $row {                          
    set cols [$root selectNodes /teryt/catalog/row/col ]
    incr licznik_row

    foreach node $cols {
        set name [$node @name]
        set val [$node text]
        puts "name=$name; val=$val"
        if { [$node @name] == "OPIS" } {                                  
             foreach n [$doc selectNodes teryt/catalog/row\[$licznik_row\]/col\[@name='OPIS'\]/linia] {
               puts "[$n asXML]" 
             } 
        }
        incr licznik
    }    
  }         
$doc delete 
and this is an output:

CODE

name=WOJ; val=04
name=OPIS; val=
<linia>WOJ 02 line 1
         </linia>
<linia>WOJ 02 line 2
         </linia>
<linia>WOJ 02 line 3
         </linia>
<linia/>
<linia/>
name=WOJ; val=02
name=OPIS; val=
<linia/>
<linia>WOJ 03 line 1
         </linia>
<linia>WOJ 03 line 2
         </linia>
(..) 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close