【发布时间】:2014-07-24 19:02:21
【问题描述】:
我正在用 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-->
【问题讨论】:
-
为什么在
imagescroller= ...中使用|?将sed命令写在单独的行中。删除/'$imagescroller'<周围的单引号。希望这会有所帮助。 -
我这样做了,结果更糟,我只有
- @drawable/
-
问题似乎来自变量$imagescroller,实际上,当用计数器$j更改它时,我有一个正常的输出!我得到了计数器的值。
标签: xml linux shell sed xmlstarlet