【问题标题】:Creating an XML Array with sed使用 sed 创建 XML 数组
【发布时间】: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'&lt; 周围的单引号。希望这会有所帮助。
  • 我这样做了,结果更糟,我只有 @drawable/
  • 问题似乎来自变量$imagescroller,实际上,当用计数器$j更改它时,我有一个正常的输出!我得到了计数器的值。

标签: xml linux shell sed xmlstarlet


【解决方案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】:

    问题是因为“。” 我删除并用节点名称替换它;那行得通:那是我的代码:

    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
    

    【讨论】:

      猜你喜欢
      • 2012-08-31
      • 2010-11-18
      • 1970-01-01
      • 2021-10-02
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      相关资源
      最近更新 更多