热门IT资讯网

bash 解析XML的脚本

发表于:2024-11-25 作者:热门IT资讯网编辑
编辑最后更新 2024年11月25日,parseXmlFile.sh#!/bin/bashUsage(){echo "Usage: ./parseXmlFile.sh "exit 1}list_all_entity(){local IFS

parseXmlFile.sh

#!/bin/bash


Usage()

{

echo "Usage: ./parseXmlFile.sh "

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


0