Documentation for the worlds leading Java Based Open Source Web Content Management System

dotCMS

XML Tool

The XML Tool

The XML Tool can be used to parse an xml file and iterate over each object returned in the xml file as needed

Class: com.dotmarketing.viewtools.xmltool.java

Name: $xmltool - (way to refer to the tool in the Velocity context)

Toolbox Configuration Example

  <tool>
    <key>xmltool</key>
    <scope>application</scope>
    <class>com.dotmarketing.viewtools.xmltool</class>
  </tool>

Here's a usage example created by Chris Falzone of the dotCMS community - Thanks Chris!

Example of a xml page that can be parsed: http://www.w3schools.com/XML/cd_catalog.xml

To parse the file/page on a dotCMS webpage, preview the following example which is doing a parse of an xml page and then iterating over 5 objects in the parse at the bottom of this document:
#set($myXML = $xmltool.read("http://www.w3schools.com/XML/cd_catalog.xml"))

#foreach($cd in $myXML.children().iterator())
  #set($cdXML = $xmltool.parse($cd))
    #if($velocityCount<=5)
      <p>
      Title: $cdXML.TITLE.text <br />
      Artist:  $cdXML.ARTIST.text <br />
      Country:  $cdXML.COUNTRY.text <br />
      Company:  $cdXML.COMPANY.text <br />
      Price:  $cdXML.PRICE.text <br />
      Year:  $cdXML.YEAR.text
      </p>
  #end
#end

dotCMS page returns the following (code above being run on this page):

Title: Empire Burlesque
Artist: Bob Dylan
Country: USA
Company: Columbia
Price: 10.90
Year: 1985

Title: Hide your heart
Artist: Bonnie Tyler
Country: UK
Company: CBS Records
Price: 9.90
Year: 1988

Title: Greatest Hits
Artist: Dolly Parton
Country: USA
Company: RCA
Price: 9.90
Year: 1982

Title: Still got the blues
Artist: Gary Moore
Country: UK
Company: Virgin records
Price: 10.20
Year: 1990

Title: Eros
Artist: Eros Ramazzotti
Country: EU
Company: BMG
Price: 9.90
Year: 1997

2

Subscribe to dotcms

Visit this group