从xml获取元素并使用shell脚本将其存储在数组中

时间:2016-03-08 20:41:24

标签: xml bash xpath sed xmllint

我的xml如下:

<URLS xmlns:"http://www.example.com">
    <Service>
        <forwardUrl>
            <value>http://www.example1.com:80</value>
            <value>http://www.example2.com:80</value>
            .
            .
            .
       </forwardUrl>
    </Service>
</URLS>

我想将所有转发网址存储在数组中。

我试过这样做:

let urlcount=$(sed -e "s/xmlns/ignore/" /tmp/in.xml | xmllint --xpath "count(//forwardUrl/value)"  -)
declare -a urls=()

for((i=1; i <= $urlcount; i++)); do
    echo $i
    urls[$i]=$(sed -e "s/xmlns/ignore/" /tmp/in.xml | xmllint --xpath '//forwardUrl/value["$i"]/text()' -)
done

但是当我echo ${urls[7]}时,它会打印所有值。

我想在不同的索引中存储不同的URL。请帮帮我。

2 个答案:

答案 0 :(得分:0)

试试这个

$ urls=($(sed -nr 's_<value>(.*)</value>_\1_p' file)); echo ${urls[1]}
http://www.example2.com:80
$ echo ${urls[0]}
http://www.example1.com:80

显然不关心xml结构,所以假设&#34;值&#34;标签仅用于网址。

更新:如果上下文对于救援重要awk

$ awk -F'[<>]' -v RS="</?forwardUrl>" 'NR==2{for(i=3;i<=NF;i+=4) print $i}' file

http://www.example1.com:80
http://www.example2.com:80

其余的是相同的

$ urls=($(awk ... )) 

请注意,此正则表达式RS特定于gawk,可能在其他awks中不受支持。

答案 1 :(得分:0)

仅使用sed

这样的事情怎么样?
$ cat file1
<URLS xmlns:"http://www.example.com">
    <Service>
        <forwardUrl>
            <value>http://www.example1.com:80</value>
            <value>http://www.example2.com:80</value>
            <value>http://www.example3.com:80</value>
            <value>http://www.example4.com:80</value>
       </forwardUrl>
    </Service>
</URLS>
$ declare -a array=($(sed -n '/\s*<forwardUrl>/,/<\/forwardUrl>/p' file1 | sed -e 's/<[^>]*>//g' -e '/^\s*$/d' -e 's/\s*//g'))
$ echo "${array[0]}"
http://www.example1.com:80
$ echo "${array[1]}"
http://www.example2.com:80
$ echo "${array[2]}"
http://www.example3.com:80
$ echo "${array[3]}"
http://www.example4.com:80
$ echo "${array[@]}"
http://www.example1.com:80 http://www.example2.com:80 http://www.example3.com:80 http://www.example4.com:80
$

表达分解:

declare -a array=($(sed -n '/\s*<forwardUrl>/,/<\/forwardUrl>/p' file1 | sed -e 's/<[^>]*>//g' -e '/^\s*$/d' -e 's/\s*//g'))
  1. sed -n '/\s*<forwardUrl>/,/<\/forwardUrl>/p' file1在符合<forwardUrl></forwardUrl>(包括首字母)的行之间打印行
  2. sed -e 's/<[^>]*>//g' -e '/^\s*$/d' -e 's/\s*//g'第一个表达式删除所有标签,第二个删除所有空行(有空格),最后一个表达式删除所有空格
  3. 编辑1:

    $ cat file1
    <URLS xmlns:"http://www.example.com">
        <Service>
            <forwardUrl>
                <value>http://www.sun.com:80</value>
                <value>http://www.example2.com:80</value>
                <value>http://www.example3.com:80</value>
                <value>http://www.example4.com:80</value>
           </forwardUrl>
        </Service>
    </URLS>
    $ declare -a array=($(sed -n '/\s*<forwardUrl>/,/<\/forwardUrl>/p' file1 | sed -e 's/<[^>]*>//g' -e '/^\s*$/d' -e 's/\s*//g'))
    $ echo "${array[0]}"
    http://www.sun.com:80
    $ echo "${array[@]}"
    http://www.sun.com:80 http://www.example2.com:80 http://www.example3.com:80 http://www.example4.com:80
    $