Ant任务检查xml文件中是否存在xml节点

时间:2013-05-20 22:30:14

标签: ant ant-contrib xmltask

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

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

我希望检查节点是否已经存在,然后我想更新此另外想要添加新节点。

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

关于, Avinash Nigam

1 个答案:

答案 0 :(得分:1)

为您的示例使用额外的根标记<foo></foo>(插入操作需要),使用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>
相关问题