-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml.sh
63 lines (53 loc) · 1.81 KB
/
xml.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env bash
#===============================================================================
#
# DESCRIPTION: to be included by other shell scripts, rudimentary XML parsing
# WARNING: bash really isn't the best tool for parsing xml
#
#===============================================================================
#-------------------------------------------------------------------------------
# A small function to help up parse the xml file by splitting stuff on < and >
#-------------------------------------------------------------------------------
parse_xml () {
local IFS=\>
local returncode=
local key=
local value=
declare -g Tag
declare -g Content
read -r -d \< Tag Content
returncode=$?
unset Attribute
declare -gA Attribute
# shellcheck disable=1101
IFS=\
for attr in ${Tag#* }
do
if [[ $attr =~ = ]]; then
key=${attr%%=*}
value=$(tidy ${attr#*=})
Attribute[${key}]=${value}
fi
done
Tag="$(trim "${Tag%% *}")"
Content="$(trim "${Content}")"
return ${returncode}
} # end of function parse_xml
#-------------------------------------------------------------------------------
# Helper functions for cleaning up the data
#-------------------------------------------------------------------------------
tidy() {
trim "${@}" | sed "s/^[\'\"]\(.*\)[\'\"]$/\1/"
} # end of function tidy
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
} # end of function trim
#-------------------------------------------------------------------------------
# Example usage
#-------------------------------------------------------------------------------
# while parse_xml ; do
# echo "-${Tag}-${Attribute[@]}-${Content}-"
# done < ${xmlfile}