2013-05-20 27 views
0

我的XML文件中,我要添加XML说Ant任务检查XML节点在XML文件中存在

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

我要检查,如果节点已经存在,那么我想更新这个otheriwse想添新。

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

与问候, 阿维纳什Nigam公司

回答

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>