Forum OpenACS Q&A: Reading a XML file from a web url

Posted by naveen cheguri on

I have a web url containing XML data in it. How can I get "formatted_address" value from the below XML URL link?

XML file URL link,%20%C4%B0stanbul,%20%2034450%20%20%20%20%20

XML data

This XML file does not appear to have any style information associated with it. The document tree is shown below.
      Selimiye Mh., Vehbi Koç Acil Tıp Mrk., 34660 Üsküdar/İstanbul, Turkey
  <route>Vehbi Koç Acil Tıp Mrk.</route>

How do my tcl file read this WEB URL and get "formatted_address" value.

Can someone help me in solving this.


Posted by Antonio Pisano on
Well, this something more related to plain tcl than OpenACS itself.

A very simple solution could be:

set formatted_address [string range $xml [expr [string first "<formatted_address>" + [string length "<formatted_address>"]] $xml] [string first "</formatted_address>" $xml]]

(didn't test, maybe there is a +1 to add somewhere in the indexes)

Other solutions could involve xml parsing, if you want to go that way, look for tdom, which is tcl's xml parsing toolkit.

As this is a very basic question for tcl, please allow me to point you to some useful resources. First one will probably give you all you need to know, even if it was written for an earlier version of tcl than current.


Posted by Neophytos Demetriou on
Off the top of my head (haven't tested, should work though)

package require tdom
set filename "yourfile"
set fp [open $filename]
set xml [read $fp]
close $fp
set doc [dom parse $xml]
set node [$doc selectNodes {//formatted_address}]
set address [$node asText]
# alternative:
# set address [$doc selectNodes{string(//formatted_address)}]
$doc delete

Posted by naveen cheguri on
Hi Neophytos

I got this error if I use dom parser. When I google this error I found that tDom is not installed.

invalid command name "dom"
while executing
"dom parse $xml"

Is there any other way I can parse my xml file


Posted by Toni Vila on
Hi Naveen,

if you want to manage xml better use tDom.

Its pretty easy intall tDom package, from openAcs Doc:

step 4.e:

After download and untar tDom package source

Install tDOM

[root nssha1]# cd ../tDOM-0.8.0/unix

Edit the CONFIG file. Uncomment the instructions meant for AOLserver 4, but edit it to look like this:

../configure --enable-threads --disable-tdomalloc
--prefix=/usr/local/aolserver --with-tcl=/usr/local/lib

Note that the location of the Tcl library may vary on differnt platforms (e.g. for Debian 3.0: --with-tcl=/usr/lib/tcl8.4)

Now you can compile and configure tDOM

[root unix]# sh CONFIG
[root unix]# make install


Posted by Gustaf Neumann on

This error message shows, that you are not using OpenACS, where tdom is preloaded. To load tDOM in a decent configured tcl shell, use "package require tdom". If tcl does not find tdom, then either you tcl-path (variable "auto_path" in Tcl) is incorrect, or you have to install tdom. On systems like ubuntu/debian, you can use "apt-get install tdom". But of course, you can get the sources and compile yourself, but for a beginner i would recommend to focus on the task.


Posted by naveen cheguri on
Thanks Everyone.

It works after installing TDom :)