Forum OpenACS Development: Tcl/XML parser problem

Collapse
Posted by Wojtek M on
Hi.
Can anybody help me resolve the problem.
This is my code:

package require tdom 0.7.5

set xml {source: http://londynek.net/image/tools/test_xml.xml }

set doc [dom parse $xml]
set root [$doc documentElement]
set teryt [$root selectNodes {/plik} ]
set dzial [$root selectNodes {/plik/lista_ofert/dzial} ]

foreach node $dzial {
set d_tab [$node @tab]
set d_typ [$node @typ]

set licznik_oferta 0
set param [$doc selectNodes {/plik/lista_ofert/dzial[@tab=$d_tab][@typ=$d_typ]/oferta[$licznik_oferta]/param}]
foreach node_param $param {
set name [$node_param @nazwa]
set val [$node_param text]

puts "name=$name; val=$val; licznik_oferta=$licznik_oferta"

if { [$node_param @nazwa] == "opis" } {
incr licznik_oferta
foreach n [$doc selectNodes {/plik/lista_ofert/dzial[@tab=$d_tab] [@typ=$d_typ]/oferta[$licznik_oferta]/param[@nazwa='opis']/linia/text()}] {
puts "[$n asXML]"
}
}
}
}
$doc delete

The output looks like that:

name=wojewodztwo; val=Mazowieckie; licznik_oferta=0
name=opis; val=; licznik_oferta=0
oferta 1111111111111
oferta 22222222222222
name=wojewodztwo; val=Lubuskie; licznik_oferta=1
name=opis; val=; licznik_oferta=1
oferta 1111111111111
oferta 22222222222222
name=cena1m; val=51; licznik_oferta=0
name=wojewodztwo; val=Estremadura; licznik_oferta=0
name=opis; val=; licznik_oferta=0
Oferta 33333333333333
Oferta 44444444444444
name=wojewodztwo; val=Mazowieckie; licznik_oferta=1
name=opis; val=; licznik_oferta=1
Oferta 33333333333333
Oferta 44444444444444

I need read information from: param nazwa="opis" linia /linia /param unfortunately the output returns all line from all rows ;/ anybody help me with this ?

Collapse
2: Re: Tcl/XML parser problem (response to 1)
Posted by Iuri Sampaio on
Hi Wojtek,

I'd go on the way using getElementsByTagName and nodeValue. As per lines bellow

###
...

set linia_nodes [$parentnode getElementsByTagName linia]

foreach node $linia_nodes {
...
set key [$node nodeName]
set value [[$node firstChild] nodeValue]
...
}

Hope that helps

Collapse
3: Re: Tcl/XML parser problem (response to 2)
Posted by Wojtek M on
I'm sorry but I don't understand what value may I put in $parentnode
Could you show me an example of my above code ?
Collapse
4: Re: Tcl/XML parser problem (response to 1)
Posted by Jim Lynch on
Hi,

Explain exactly the purpose of the line that starts "set xml". Specifically, what is expected to be in $xml immediately after that line is run

Collapse
5: Re: Tcl/XML parser problem (response to 1)
Posted by Jim Lynch on
One technique that has never failed me, is to write the script, and check each line, and print something... so, your first script might be something like...

package require tdom 0.7.5

set xml {source: http://londynek.net/image/tools/test_xml.xml }

set doc [dom parse $xml]

# here, you can use puts if you're running this script from
# the shell... then you might see the problem right away:

puts "value of doc is $doc"

# or, if this is the .tcl part of an openacs templated page,
# you'd put this:

ad_page_contract {
doc goes here
} -properties {
doc : onevalue
}

# before everything in the script, then you'd put this
# in the corresponding .adp:

the value of $doc is: @doc@

# then load the page, and then see what happened. I would
# recommend that you start by running it from the shell,
# then you can clearly see the tcl error output if any.

Collapse
6: Re: Tcl/XML parser problem (response to 1)
Posted by Jim Lynch on
the line you'd be checking there is the first one that used tdom, i.e., set doc [dom parse $xml].

Once you're satisfied that the line worked, go to the next, add it to your test script, don't forget the line that puts it out so you can see it.

The idea is you make sure the line is producing the exact behavior you expect before going on to the next.

Other things you can print about these objects, is their object type, which as part of checking for the behavior you want, you'd check the type so you know it's handing you back an object of the type you expect.

Collapse
7: Re: Tcl/XML parser problem (response to 1)
Posted by bikitha lee on