parseXmlFile.sh

#!/bin/bash


Usage()

{

        echo "Usage: ./parseXmlFile.sh <target.xml>"

        exit 1

}



list_all_entity()

{

        local IFS=\>


        while read -d \< ENTITY CONTENT

        do

                echo $ENTITY

        done < $1

}


list_all_content()

{

        local IFS=\>


        while read -d \< ENTITY CONTENT

        do

                echo $CONTENT

        done < $1

}


list_all_tag_name()

{

        local IFS=\>


        while read -d \< ENTITY CONTENT

        do

                TAG_NAME=${ENTITY%% *}

                echo $TAG_NAME

        done < $1

}


list_all_attributes()

{

        local IFS=\>


        while read -d \< ENTITY CONTENT

        do

                ATTRIBUTES=${ENTITY#* *}

                echo $ATTRIBUTES

        done < $1

}


getattr()

{

        ATTR_PAIR=${1#*$2=\"}

        echo "${ATTR_PAIR%%\"*}"

}


get_attributes_value()

{

        local IFS=\>


        while read -d \< ENTITY CONTENT

        do

                TAG_NAME=${ENTITY%% *}

                ATTRIBUTES=${ENTITY#* *}

                if [[ $TAG_NAME == $2 ]]

                then

                        value=`getattr ${ATTRIBUTES} ${3}`

                        echo $value

                fi

        done < $1

}


get_content_value()

{

        local IFS=\>


        while read -d \< ENTITY CONTENT

        do

                TAG_NAME=${ENTITY%% *}

                if [[ $TAG_NAME == $2 ]]

                then

                        echo $CONTENT

                fi

        done < $1

}



#main in this shell

if [ $# -ne 1 ]

then

       Usage

fi


if [ ! -f $1 ]

then

       echo "fail to load data from file, file $1 not exist!"

       exit 1

fi


list_content $1