LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Processing XML file (reading records) (https://www.linuxquestions.org/questions/linux-newbie-8/processing-xml-file-reading-records-652170/)

lourencojunior 06-27-2008 06:16 PM

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

j-ray 06-28-2008 08:56 AM

probably best way to use perl or python,
http://oreilly.com/catalog/pythonxml/chapter/ch01.html

lourencojunior 06-28-2008 01:20 PM

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


All times are GMT -5. The time now is 02:31 PM.