子节点值的XML显示属性

时间:2014-03-26 16:00:34

标签: xml ant

我有一个像这样的文件rapport.xml:

<?xml version="1.0"?>
<group difficult="easy" time="43" timestamp="2014-03-26T16-09-14">
    <step num="280" date="2014/03/26 - 16:09:14" time="21">
        <example job="secretary" partener="sct">
            The fish
        </example>
        <example job="bodyguard" partener="bdg">
            The squirrel
        </example>
    </step>
    <step num="600" date="2014/03/26 - 16:09:36" time="22">
        <example job="lifeguard" partener="lfg">
              The cat
        </example>
        <example job="teacher" partener="tcr">
              The dog
        </example>
    </step>
</group>

我问这个文件是否有另一个xml文件。

<xmlproperty file="rapport.xml" prefix="PREFIX" collapseAttributes="true"/>
<var name="mail1" value="${PREFIX.group.step.num}"/>
<echo message="Mail : ${line.separator} ${mail1}" />

我想展示:

  

第280步

     工作:秘书

     

例子:鱼

     

工作:保镖

     

示例:松鼠

     

第600步

     工作:救生员

     

例子:猫

     

工作:老师

     

示例:狗

请帮帮我,谢谢

1 个答案:

答案 0 :(得分:1)

使用脚本和内置JavaScript引擎的低级方法将类似于:

<project>

 <xmlproperty file="rapport.xml" collapseattributes="true"/>

 <!--
 [echoproperties] group.difficult=easy
 [echoproperties] group.step.date=2014/03/26 - 16\:09\:14,2014/03/26 - 16\:09\:36
 [echoproperties] group.step.example=The fish,The squirrel,The cat,The dog
 [echoproperties] group.step.example.job=secretary,bodyguard,lifeguard,teacher
 [echoproperties] group.step.example.partener=sct,bdg,lfg,tcr
 [echoproperties] group.step.num=280,600
 [echoproperties] group.step.time=21,22
 [echoproperties] group.time=43
 [echoproperties] group.timestamp=2
 -->

<script language="javascript">
 var steps = project.getProperty('group.step.num').split(',');
 var jobs = project.getProperty('group.step.example.job').split(',');
 var examples = project.getProperty('group.step.example').split(',');

 println('step ' + steps[0]);
 println('job: ' + jobs[0]);
 println('example: ' + examples[0]);
 println('job: ' + jobs[1]);
 println('example: ' + examples[1]); 
 println('step ' +steps[1]);
 println('job: ' + jobs[2]);
 println('example: ' + examples[2]);
 println('job: ' + jobs[3]);
 println('example: ' + examples[3]);
</script>

</project>

输出:

   [script] step 280
   [script] job: secretary
   [script] example: The fish
   [script] job: bodyguard
   [script] example: The squirrel
   [script] step 600
   [script] job: lifeguard
   [script] example: The cat
   [script] job: teacher
   [script] example: undefined

否则使用xmltask(推荐用于任何xml相关的东西和xml驱动的构建),f.e。 :

<project>

<!-- Import XMLTask -->
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>

<xmltask source="path/to/rapport.xml" report="true">
 <call path="//group/step">
   <param name="step" path="@num"/>
   <param name="job1" path="example[1]/@job"/>
   <param name="example1" path="example[1]/text()"/>
   <param name="job2" path="example[2]/@job"/>
   <param name="example2" path="example[2]/text()"/>

 <actions>
  <echo>
   step @{step}
   job : @{job1}
   example : @{example1}
   job : @{job2}
   example : @{example2}
  </echo>
 </actions>
 </call>
</xmltask>

</project>

输出:

     [echo] step 280
     [echo] job : secretary
     [echo] example : The fish
     [echo] job : bodyguard
     [echo] example : The squirrel
     [echo] step 600
     [echo] job : lifeguard
     [echo] example : The cat
     [echo] job : teacher
     [echo] example : The dog
  [xmltask] Document -->
<group difficult="easy" time="43" timestamp="2014-03-26T16-09-14">
    <step date="2014/03/26 - 16:09:14" num="280" time="21">
        <example job="secretary" partener="sct">The fish</example>
        <example job="bodyguard" partener="bdg">The squirrel</example>
    </step>
    <step date="2014/03/26 - 16:09:36" num="600" time="22">
        <example job="lifeguard" partener="lfg">The cat</example>
        <example job="teacher" partener="tcr">The dog</example>
    </step>
</group>
  [xmltask] Document <--
BUILD SUCCESSFUL

注意:
你应该改变你的xml格式:

<example job="secretary" partener="sct">
    The fish
</example>

到:

<example job="secretary" partener="sct">The fish</example>

正如我所做的那样=&gt;请参阅包含xml文档的xmltask示例的输出,因为使用了属性report="true" 否则你的输出将会混乱。