xml - 使用 sed 创建 XML 数组

标签 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,我在评论中使用它,就像标签一样,因为我需要按顺序在该标签之前插入我的项目。

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/

相关文章:

Python XML解析ElementTree复杂的XML结构

xml - XPATH 按位置选择返回不连贯的结果

html - 使用正则表达式查找嵌套的 XML 标记

linux - 如何找到printf(库函数)的堆栈大小?

java - 哪个在 Tomcat、Jboss AS 和 Glassfish 中表现更好?

linux - Linux 的最大管道命令

xml - 将命名空间添加到 xml 时使用 xslt 解析 xml 失败

mysql - 错误 1045 (28000) : Unknown error 1045

linux - Makefile 和 Shell 脚本 (Bash)

file - 从命令行将文本写入 Hadoop