stella06700
stella06700

Reputation: 77

XML display attribute of the child node value

I have a file rapport.xml like this:

<?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>

I ask this file with another xml file.

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

I would like to display :

step 280

job : secretary

example : the fish

job : bodyguard

example : the squirrel

step 600

job : lifeguard

example : the cat

job : teacher

example : the dog

Can you help me please, thank you

Upvotes: 0

Views: 563

Answers (1)

Rebse
Rebse

Reputation: 10377

A lowlevel approach with script and builtin javascript engine would be something like :

<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>

output :

   [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

Otherwise use xmltask (recommended for any xml related stuff and xml driven builds), 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>

output :

     [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

Note :
you should change the format of your xml from :

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

to :

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

as i did => see output from xmltask example which contains xml document because attribute report="true" is used.
Otherwise your output will be cluttered.

Upvotes: 1

Related Questions