Xml Tool - Iteration with Attributes

14 views
Skip to first unread message

Alex

unread,
Dec 24, 2018, 7:45:51 AM12/24/18
to dotCMS User Group
I have the following code and questions:

#set($myXML = $xmltool.read("https://xxx.xxx.com/test.xml"))



Here is the XML document:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<forecast>
<location id="MROC">
        <day name="Wednesday">
            <high unit="C">8</high>
            <low unit="C">6</low>
            <icon>18</icon>
            <windspeed unit="kph">42.0</windspeed>
            <wind_dir>E</wind_dir>
            <hdd>20</hdd>
            <cdd>0</cdd>
            <phrase>Rain, some heavy; winds will be locally damaging</phrase>
            <US>Cloudy</US>
        </day>

     <day name="Thursday">
            <high unit="C">8</high>
            <low unit="C">6</low>
            <icon>18</icon>
            <windspeed unit="kph">42.0</windspeed>
            <wind_dir>E</wind_dir>
            <hdd>20</hdd>
            <cdd>0</cdd>
            <phrase>Rain, some heavy; winds will be locally damaging</phrase>
            <US>Cloudy</US>
        </day>
. .
</location>

<location id="IRCS">
        <day name="Wednesday">
etc.
</location>
.
.
.
.
</forecast>

## Select the element with the attribute I want
##
#set($itemlist= $myXML.find('/forecast/location[@id=\'MROC\']'))


##
## Since we already found the location node what is to follow is to itirate over the children
### 
#foreach($items in $itemlist.iterator()) ##this is correct and gives me the right string with all the information
#set($item = $xmltool.parse($items))

## Note the $item.size is 1.  It gives me one string with all the following information:

##8 6 18 42.0 E 20 0 Rain, some heavy; winds will be locally damaging Cloudy 9 7 19 43.0 E1 21 2 Rain Cloudy 10 8 20 44.0 E2 22 2 winds Cloudy 11 9 21 46.0 E4 24 42 winds will be locally damaging Cloudy ...

##etc.

##
## To access the data on each location I have to do:  

<td><strong>Day Name 1:</strong> $!{item.day.name}</td> ----prints out ---> Wednesday
<td><strong>HDD:</strong> $!{item.day.hdd[0].text}</td> ----prints out ---> 20
<td><strong>Day Name 2:</strong> $!{item.day[1].name}</td>  ----prints out ---> Thursday
#end

My question is ----> With the above approach I have to write the whole HTML document and put each individual variable in its place:  $$!{item.day[1].name}, etc.

Is there a way, I can itirate over each segment (8 6 18 42.0 E 20 0 Rain, some heavy; winds will be locally damaging Cloudy) of the above results, keeping their consistency and just print out each child of the list?

Thanks,

Alex


Reply all
Reply to author
Forward
0 new messages