我正在用 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,我在评论中使用它,就像标签一样,因为我需要按顺序在该标签之前插入我的项目。
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-->
最佳答案
xmlstarlet sel -t \
-e array \
-a name -o SERVICES -b \
-m //imageName \
-e item -v 'concat("@drawable/", .)' -b \
/home/wissem/Bureau/app.xml
关于xml - 使用 sed 创建 XML 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24032183/