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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。