LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-27-2008, 06:16 PM   #1
lourencojunior
LQ Newbie
 
Registered: May 2008
Posts: 8

Rep: Reputation: 0
Question Processing XML file (reading records)


Hi everyone!

I have a doubt about reading records in a XML file. For example, I have a xml file where it's stored a menu which describes what will be served each day. So, I would like to creat a Bash or Python script for getting information about this xml file.

XML example:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<restaurant>
	<monday>
		<date>
		23/06/2008</date>
		<lunch>
			<salad>
			There is no salad.</salad>
			<main>
	
			Beef with four cheese</main>
			<extras>
	
			Chocolate</extras>
			<dessert>
	
			Chocolate</dessert>
		</lunch>
		<dinner>
			<salad>
				There is no salad.</salad>
			<main>
	
			Chicken</main>
			<extras>
	
			Apple</extras>
			<dessert>
	
			Apple</dessert>
		</dinner>
	</monday>
	<tuesday>
		<date>
		24/06/2008</date>
		<lunch>
			<salad>
			There is no salad.</salad>
			<main>
			
			Beef</main>
			<extras>
			
			Orange</extras>
			<dessert>
			
			Orange</dessert>
		</lunch>
		<dinner>
			<salad>
			There is no salad.</salad>
			<main>
			
			Beef</main>
			<extras>
			
			Yogurte</extras>
			<dessert>
			
			Yogurte</dessert>
		</dinner>
	</tuesday>
	<wednesday>
		<date>
		25/06/2008</date>
		<lunch>
			<salad>
			There is no salad.</salad>
			<main>
			
			Fish</main>
			<extras>
			
			Pineapple</extras>
			<dessert>
			
			Pineapple</dessert>
		</lunch>
		<dinner>
			<salad>
			There is no salad.</salad>
			<main>
			
			Chicken</main>
			<extras>
			
			Chocolate</extras>
			<dessert>
			
			Chocolate</dessert>
		</dinner>
	</wednesday>
	<thursday>
		<date>
		26/06/2008</date>
		<lunch>
			<salad>
			There is no salad.</salad>
			<main>
			
			Feijoada completa</main>
			<extras>
			
			Laranja</extras>
			<dessert>
			
			Laranja</dessert>
		</lunch>
		<dinner>
			<salad>
			There is no salad.</salad>
			<main>
			
			Bife acebolado</main>
			<extras>
			
			Mamão</extras>
			<dessert>
			
			Mamão</dessert>
		</dinner>
	</thursday>
	<friday>
		<date>
		27/06/2008</date>
		<lunch>
			<salad>
			There is no salad.</salad>
			<main>
			
			Chicken</main>
			<extras>
			
			Yogurte</extras>
			<dessert>
			
			Yogurte</dessert>
		</lunch>
		<dinner>
			<salad>
			There is no salad.</salad>
			<main>
			
			Hamburger</main>
			<extras>
			
			Banana</extras>
			<dessert>
			
			Banana</dessert>
		</dinner>
	</friday>
	<saturday>
		<date>
		28/06/2008</date>
		<lunch>
			<salad>
			There is no salad.</salad>
			<main>
			
			Vegetables</main>
			<extras>
			Chocolate</extras>
			<dessert>
			
			Chocolate</dessert>
		</lunch>
		<dinner>
			<empty />
		</dinner>
	</saturday>
</restaurant>
Now, let's say I would like to know what will be served on saturday. I would get this by using:
Code:
$ menu.university saturday
Or, I would like to know what will be served for lunch on monday. I would get this by using:
Code:
$ menu.university monday lunch

Does anybody have any insight for helping me coding this?

Thanks in advance.
[]s
 
Old 06-28-2008, 08:56 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
probably best way to use perl or python,
http://oreilly.com/catalog/pythonxml/chapter/ch01.html
 
Old 06-28-2008, 01:20 PM   #3
lourencojunior
LQ Newbie
 
Registered: May 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Smile

Thank you j-ray!!

I will read about Python and those APIs mentioned in such chapter as well as download the sample codes available there.

Well, I just coded a solution for now. It works fine, but I must improve it. Ah.. and the code is quit big yet.

Code:
#! /bin/bash
#
# COPYRIGHT:
# (c) 2008 Lourenco Alves Pereira Junior (GNU LGPL V2.1)
# You may view the full copyright text at:
# http://www.opensource.org/licenses/lgpl-license.html
#
# DESCRIPTION:
# Alpha version!
# 
# Description coming soon!!
# 
#

MENU=http://www.pcasc.usp.br/restaurante.xml
WEEKDAY=
DATE=
SED_FILTER=
# SED="sed $SED_FILTER"

function usage
{
	local BIN=$(basename $0)
	echo -e "Usage:"
	echo -e "\t$BIN <weekday> [<meal>]"
	echo -e "Where:"
	echo -e "\t<weekday>: can be 'segunda', 'terca', 'quarta', 'quinta', 'sexta' ou 'sabado'."
	echo -e "\t<meal>: which meal you want to see. It can be either 'almoco' or 'jantar'"
	echo -e "Examples:"
	echo -e "\t$BIN segunda"
	echo -e "\t$BIN sabado"
	echo -e "\t$BIN terca almoco"
	echo -e "\t$BIN quarta jantar"
	echo -e ""
	echo -e "This is an alpha version! It will be improved."
	exit 1
}

function filter
{
	SED_FILTER="s/.*<$1>\(.*\)<\/$1>.*/\1/g"
	return
}

# Usage: get_meals $WEEKDAY
function get_meals
{
	filter $1
	WEEKDAY=$(lynx -source $MENU | tr -d '\n' | tr -d '\r' | sed $SED_FILTER)
# 	echo -e $WEEKDAY
	return
}

# Usage: print_date $WEEKDAY
function print_date
{
	local date

	filter data
	date=$(echo $1 | sed $SED_FILTER)
	echo -e 'Data: \t\t'$date
	return
}

# Usage: print_meal $WEEKDAY $MEAL
function print_meal
{
# 	echo 'print_meal 1: '$1
# 	echo 'print_meal 2: '$2
# 	return

	local weekday_meals=$1
	local meal=$2

# 	echo 'print_meal weeday_meals: '$weekday_meals
# 	echo 'print_meal meal: '$meal
# 	return

	local lunch salad main side_order dessert

	filter $meal
	lunch=$(echo $weekday_meals | sed $SED_FILTER)
# 	echo -e $lunch
# 	return
	
	filter salada
	salad=$(echo $lunch | sed $SED_FILTER)
	echo -e 'Salada: \t'$salad
	
	filter principal
	main=$(echo $lunch | sed $SED_FILTER)
	echo -e 'Principal: \t'$main
	
	filter acompanhamento
	side_order=$(echo $lunch | sed $SED_FILTER)
	echo -e 'Acompanhamento: '$side_order
	
	filter sobremesa
	dessert=$(echo $lunch | sed $SED_FILTER)
	echo -e 'Sobremesa: \t'$dessert

	return
}

function main
{
	# Checking the params
	if (( $# < 1 || $# > 2 ))
	then
		usage
	fi

# 	echo $1
# 	echo $2
# 	return

	# Getting the weekday meals
	get_meals $1
	print_date "$WEEKDAY"
	echo ""

	if (( $# == 1 ))
	then # print both meals: lunch (almoco) and dinner (jantar)
		echo "Almoco:"
		print_meal "$WEEKDAY" almoco
		echo ""
		echo "Jantar:"
		print_meal "$WEEKDAY" jantar
	else # print specified meal
# 		echo 'Main weekday: '$WEEKDAY
# 		echo 'Main 2: '$2
# 		return
		local meal=$2
# 		echo $meal
# 		return
		print_meal "$WEEKDAY" $meal
	fi
}


main $@

# END OF FILE
Any insights are always welcomed!
[]s
 
  


Reply

Tags
bash, python, xml


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Yast2 and Secondary reading zone records swamprat SUSE / openSUSE 1 10-10-2007 10:40 PM
Processing XML in Erlang lilaz Programming 0 08-21-2007 02:05 PM
Reading/Wirting file/parsing xml file using javascript fakhrul Programming 1 08-14-2007 05:08 PM
reading xml Armon Programming 2 01-12-2006 11:29 PM
rpm hangs while reading/processing headers after a forced removing of files hamtavs Linux - Software 0 08-25-2004 02:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration