Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tcl/XML parser problem

Status
Not open for further replies.

v0jt4s

Programmer
Apr 4, 2013
3
PL
Hi.
Can anybody help me resolve the problem.
This is my code:
Code:
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 ?

 
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
 
This is not exacly what I looking for. I found a solution but they give me too much data ;/
Code:
(..)
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 ??
 
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>
(..)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top