【问题标题】:Ant task to check that xml node exists in xml fileAnt 任务检查 xml 文件中是否存在 xml 节点
【发布时间】:2013-05-20 22:30:13
【问题描述】:

我里面有xml文件,我想添加xml说

<car name="BMW">
   <color>Red</color>
   <model>x3</model>
   </car>

我想检查节点是否已经存在然后我想更新这个其他想添加新的。

我对 ant xmltask 很陌生,所以我的问题可能很简单。

关于, 阿维纳什·尼加姆

【问题讨论】:

    标签: ant ant-contrib xmltask


    【解决方案1】:

    为您的示例使用额外的根标签&lt;foo&gt;&lt;/foo&gt;(插入操作所需),
    您可以使用xmltask =

    <!-- edit file in place, use other dest if you need to create a new file -->
    <xmltask source="path/to/file.xml" dest="path/to/file.xml">
    <!-- create property if car node with name='BMW' exists -->
    <copy path="//car[@name='BMW']/text()" property="modelexists"/>
    <!-- insert new car node if car node with name='BMW' doesn't exist -->
    <insert path="/foo" unless="modelexists">
     <![CDATA[
     <car name="BMW">
      <color>Red</color>
      <model>x3</model>
     </car>
     ]]>
    </insert>
    <!-- replace car node if car node with name='BMW' exists -->
    <replace path="//car[@name='BMW']" if="modelexists">
     <![CDATA[
     <car name="BMW">
      <color>Blue</color>
      <model>x4</model>
     </car>
     ]]>
    </replace>
    </xmltask>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 2011-04-08
      相关资源
      最近更新 更多