Sponsored Content
Top Forums Shell Programming and Scripting xmlstarlet template parse small xml file Post 302420428 by unclecameron on Tuesday 11th of May 2010 02:38:28 PM
Old 05-11-2010
got it!

here's my working code:
Code:
lat="42.163485"
lon="-123.646759"
url="http://ws.geonames.org/findNearbyPlaceName?lat=$lat&lng=$lon"
curl -s "$url" > xmlfile
city=`/usr/bin/xmlstarlet sel -t -m //geoname -v toponymName xmlfile`
country_code=`/usr/bin/xmlstarlet sel -t -m //geoname -v countryCode xmlfile`
echo "city $city"
echo "country_code: $country_code"

what I learned was:

1. sel - that's what you want if you want to query something from your xml file
2. -t - the template is that you specify something you want to match, which is why you have -m next
3. -m - that's the match part, what you want to match, or try to find. In order to match something, you have to know something about the structure of your xml file, to tell xmlstarlet where to go looking. In my case, it had to start looking in the element <geoname>, since that's where the child element I want lives. You can find out your structure by running xmlstarlet el xmlfile, that will tell you the element structure without the gobblygook.
4. -v - this is what you're actually searching for, in my case what's in the child element <toponymName>
5. file - your source xml file

Feel free to correct me if any of this is wrong, just trying to learn and then simplify it for the next guy Smilie
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to parse a XML file using PERL and XML::DOm

I need to know the way. I have got parsing down some nodes. But I was unable to get the child node perfectly. If you have code please send it. It will be very useful for me. (0 Replies)
Discussion started by: girigopal
0 Replies

2. Shell Programming and Scripting

Parse XML file

Hi, I need to parse the following XML data enclosed in <a> </a> XML tag using shell script. <X> ..... </X> <a> <b> <c>data1</c> <c>data2</c> </b> <d> <c>data3</c> </d> </a> <XX> ... </XX> (5 Replies)
Discussion started by: viki
5 Replies

3. Shell Programming and Scripting

How can I parse xml file?

How can I parse file containing xml ? I am sure that its best to use perl - but my perl is not very good - can someone help? Example below contents of file containing the xml - I basically want to parse the file and have each field contained in a variable.. ie. I want to store the account... (14 Replies)
Discussion started by: frustrated1
14 Replies

4. Emergency UNIX and Linux Support

How to parse the following xml file

Hi, I have the following file Example.xml <?xml version="1.0" encoding="iso-8859-1"?> <html><set label="09/07/29" value="1241.90"/> </html> Can any one help me in parsing this xml file I want to retrive the attribute values of the tag set Example I want to... (3 Replies)
Discussion started by: Raji_gadam
3 Replies

5. Shell Programming and Scripting

sed parse small xml file

I have a tmp.xml file like: <?xml version="1.0" encoding="UTF-8"?> <Response> <Ip>193.143.121.198</Ip> <Status>OK</Status> <CountryCode>PL</CountryCode> <CountryName>Poland</CountryName> <RegionCode>82</RegionCode> <RegionName>Pomorskie</RegionName> <City>Gdansk</City> ... (9 Replies)
Discussion started by: unclecameron
9 Replies

6. Shell Programming and Scripting

xmlstarlet parse field from file

I have a xmlfile like this: <?xml version="1.0" encoding="utf-8"?> <contentlocation xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="http://wherein.yahooapis.com/v1/schema" xml:lang="en"> <processingTime>0.001538</processingTime> ... (1 Reply)
Discussion started by: unclecameron
1 Replies

7. Shell Programming and Scripting

xmlstarlet parse non en_US characters

I'm parsing around 600K xml files, with roughly 1500 lines of text in each, some of the lines include Chinese, Russian, whatever language, with a bash script that uses cat $i | xmlstarlet sel -t -m "//section1/section2/section3/section4/section5" -v "@VALUE" -n > somefile which works, but I... (15 Replies)
Discussion started by: unclecameron
15 Replies

8. Shell Programming and Scripting

Creating a larger .xml file from a template(sample file)

Dear All, I have a template xml file like below. ....Some---Header....... <SignalPreference> ... <SignalName>STRING</SignalName> ... </SignalPreference> ......Some formatting text....... <SignalPreference> ......... ... (3 Replies)
Discussion started by: ks_reddy
3 Replies

9. UNIX for Dummies Questions & Answers

XML File Generation - Template Help

Hi, I have hit a bit of a brick wall.:confused: need the following code edited: echo "<?xml version=\"1.0\"?><dailyBalance_ROWSET>" > ${DataDir}/${extract_script}${ApplicationDate}.${Suffix} RunSQL ${extract_script} ${ActionFlag} echo "</dailyBalance_ROWSET>" >>... (2 Replies)
Discussion started by: Xergxes7
2 Replies

10. UNIX for Beginners Questions & Answers

How to insert subnode in xml file using xmlstarlet or any other bash command?

I have multiple xml files where i want to update a subnode if the subnode project points to different project or insert a subnode if it doesn't exist using a xmlstarlet or any other command that can be used in a bash script. I have been able to update the subnode project if it doesn't point to... (1 Reply)
Discussion started by: Sekhar419
1 Replies
lat(1)								Linux User's Manual							    lat(1)

NAME
lat - LDAP Administration Tool SYNOPSIS
lat [options] DESCRIPTION
lat, stands for LDAP Administration Tool. The tool allows you to browse LDAP-based directories and add/edit/delete entries contained within. It can store profiles for quick access to different servers. There are also different views available such as Users, Groups and Hosts which allow you to easily manage objects without having to deal with the intricacies of LDAP. OPTIONS
lat has few options, they are listed below. -d, --debug Run the program with debugging code turned on. -h, --help Print the program usage and exit. -v, --version Print the program version and exit. FILES
$HOME/.lat/profiles.xml Contains the users saved connection profiles. There is no password information held in the files; Passwords are stored in the users GNOME keyring database. $HOME/.lat/plugins.state Contains the user configuration for LAT plugins $HOME/.lat/profiles.xml Contains information on user connection profiles $HOME/.lat/templates.dat Contains information on the users custom templates. AUTHOR
Loren Bandiera <lorenb@mmgsecurity.com> REPORTING BUGS
Report bugs to http://dev.mmgsecurity.com/bugs/ COPYRIGHT
Copyright (C) 2005 MMG Security, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICU- LAR PURPOSE. lat May 2005 lat(1)
All times are GMT -4. The time now is 03:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy