Back

Handling XML Feeds with Children and Namespaces

Description

The code below is a quick example on how to handle nested children in a simple XML feed and how to handle namespace fields that have a ":" in them.

Here is a link to xpath sytnax for those not familiar: http://www.w3schools.com/xpath/xpath_syntax.asp

Here is a link to the XMLtool documentation in dotCMS: http://dotcms.com/docs/latest/XMLTool

Code

#set($myXML = $xmltool.read("http://fruitfromthevine.wordpress.com/feed/"))

<h2>Fruit From the Vine XML Feed</h2>
<table  border="1" style="width:100%;">
  <tr>
    <th><h2>Title</h2></th>
    <th><h2>Link</h2></th>
    <th><h2>Description</h2></th>
    <th><h2>Update Period</h2></th>
    <th><h2>Update Frequency</h2></th>
  </tr>

     <tr>
      <td>
         $myXML.find("/rss/channel/title").text</td>
      <td>
         $myXML.find("/rss/channel/link").text</td>
      <td>
         $myXML.find("/rss/channel/description").text</td>
      <td>
         $myXML.find("/rss/channel/sy:updatePeriod").text</td>
      <td>
          $myXML.find("/rss/channel/sy:updateFrequency").text</td>
     </tr>
</table>

<h2>Articles</h2>  
#set($itemlist= $myXML.find("/rss/channel/item"))

#foreach($items in $itemlist.iterator())

#set($item = $xmltool.parse($items))

##Use the escape html tool to see all the fields, debug, etc. as needed
##$esc.html($item)

<table border="1" style="width:100%;">
  <tr>
    <th><h3>Title</h3></th>
    <th><h3>Link</h3></th>
  </tr>

<a href="/test/test-maps.html"> </a>
     <tr>
      <td style="width:30%;">$item.find("/item/title").text</td>
      <td><a href="${item.find("/item/link").text}" target="blank">$item.find("/item/link").text</a></td>
     </tr>
</table>

#end