使用sed创建XML数组

时间:2021-12-23 15:31:13

i'm parsing an xml file with XMLStarlet ; then i need to set that data in an xml array into another xml file . the problem is that i didn't get the data on my final xml.

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

this is the xml that i'm parsing :

这是我正在解析的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>

this is my script to parse this :

这是我解析这个的脚本:

PS :servicetitle = SERVICES, i use it in a coomment ,like a tag, because i need to insert my items just before that tag , in order.

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;;

what i have on output :

我对输出的看法:

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

what it should be :

应该是什么:

  <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 个解决方案

#1


1  

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

#2


0  

the problem was because of the "." i removed and replace it with the node name ; that worked :and that's my code :

问题是因为“。”我删除并用节点名称替换它;有效:这是我的代码:

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

#1


1  

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

#2


0  

the problem was because of the "." i removed and replace it with the node name ; that worked :and that's my code :

问题是因为“。”我删除并用节点名称替换它;有效:这是我的代码:

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