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

Collapse
Posted by naveen cheguri on
Hi

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
http://ssgdev.xxx.yyy.com/maps/GoogleGeocodingV3?address=Ko%C3%A7%20%C3%BCniversitesi%20yurdu,%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.
<GeocodeResponse>
<status>OK</status>
<result>
  <geometry>
    <location>
      <lat>41.0050665</lat>
      <lng>29.0242677</lng>
    </location>
    <location_type>LOCALITY</location_type>
  </geometry>
  <formatted_address>
      Selimiye Mh., Vehbi Koç Acil Tıp Mrk., 34660 Üsküdar/İstanbul, Turkey
  </formatted_address>
  <route>Vehbi Koç Acil Tıp Mrk.</route>
  <locality>İstanbul</locality>
<administrative_area_level_1>İstanbul</administrative_area_level_1>
  <country>TR</country>
  <postal_code>34660</postal_code>
</result>
</GeocodeResponse>

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

Can someone help me in solving this.

Thanks

Collapse
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.

1) http://philip.greenspun.com/tcl/
2) http://wiki.tcl.tk/
3) http://tmml.sourceforge.net/doc/tcl/

Collapse
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

Collapse
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

Thanks

Collapse
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:

http://www.openacs.org/doc/aolserver4.html

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

cheers!

Collapse
Posted by Gustaf Neumann on
Naveen,

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.

-gn

Collapse
Posted by naveen cheguri on
Thanks Everyone.

It works after installing TDom :)