使用sed创建XML数组

时间:2014-06-04 08:10:20

标签: xml linux shell sed xmlstarlet

我正在使用XMLStarlet解析xml文件;然后我需要将xml数组中的数据设置为另一个xml文件。 问题是我没有在最终的xml上获取数据。

这是我正在解析的xml:

    <service>
        <position>3</position>
        <serviceType>DescriptionScrollImages</serviceType>
        <icon>gallerie_icon_.png</icon>
        <title>SERVICES</title>
        <colorTitle>#e2d5b2</colorTitle>
        <description>cipaux centres d’intérêt : des monuments, des musées ainsi que le is des congrès.</description>
        <imageScroll>
            <imageName>Photo_Gallerie_1.png</imageName>
        </imageScroll>
        <imageScroll>
            <imageName>Photo_Gallerie_2.png</imageName>
        </imageScroll>
        <imageScroll>
            <imageName>Photo_Gallerie_3.jpg</imageName>
        </imageScroll>
    </service>

这是我解析此脚本的脚本:

PS:servicetitle = SERVICES ,我在coomment中使用它,比如标记,因为我需要按顺序在该标记之前插入我的项目。

s=`xmlstarlet sel -t -v "count(/root/services/service/imageScroll)" /home/wissem/Bureau/app.xml`

for j in `seq 1 $((s))`;
do
imagescroller=`xmlstarlet sel -t -m "//root/services/service/imageScroll[$j] " -v "."  -n /home/wissem/Bureau/app.xml`| sed -i "/<\!\-\-"$servicetitle"\-\->/ i\<item>\@drawable\/'$imagescroller'<\/item>" /root/AndroidStudioProjects/RevolutionApp_T1/app/src/main/res/values/imageviewer.xml;
cp $imagescroller /root/AndroidStudioProjects/RevolutionApp_T1/app/src/main/re/drawable-hdpi/

 done;;

我对输出的看法:

<array name="SERVICES">
  <item>@drawable/''</item>
  <item>@drawable/''</item>
  <item>@drawable/''</item>
  <!--SERVICES-->
</array>
<!--ENDTAG-->

应该是什么:

  <array name="SERVICES">
   <item>@drawable/Photo_Gallerie_1.png</item>
   <item>@drawable/Photo_Gallerie_2.png</item>
   <item>@drawable/Photo_Gallerie_3.png</item>
  <!--SERVICES-->
 </array>
 <!--ENDTAG-->

2 个答案:

答案 0 :(得分:1)

 xmlstarlet sel -t \
  -e array \
     -a name -o SERVICES -b \
     -m //imageName  \
       -e item -v 'concat("@drawable/", .)' -b \
  /home/wissem/Bureau/app.xml

答案 1 :(得分:0)

问题是因为&#34;。&#34; 我删除并用节点名称替换它;有效:那是我的代码:

for j in `seq 1 $((s))`;
do
imagescroller=`xmlstarlet sel -t -m "//root/services/service/imageScroll[$j]" -v imageName -n /home/wissem/Bureau/application.xml`;
sed -i "/<\!\-\-"$servicetitle"\-\->/ i\<item>\@drawable\/$imagescroller<\/item>" /root/AndroidStudioProjects/RevApp/app/src/main/res/values/imageviewer.xml;
done