XMLTool
The XMLTool can be used to parse an remote XML file and iterate over each object returned in the XML file as needed
Class: com.dotmarketing.viewtools.xmltool.java
Name: ${esc.d}xmltool
Toolbox Configuration Example:
<tool> <key>xmltool</key> <scope>application</scope> <class>com.dotmarketing.viewtools.xmltool</class> </tool>
Example#
Here's a usage example created by Chris Falzone of the dotCMS community. Thanks, Chris!
First, we'll start with an example of an XML page that can be parsed: http://www.w3schools.com/XML/cd_catalog.xml
To parse the XML file on a dotCMS page, preview the following example which is doing a parse of an XML page and then iterating over five objects in the parse at the bottom of this document:
${esc.h}set(${esc.d}myXML = ${esc.d}xmltool.read("https://www.w3schools.com/XML/cd_catalog.xml")) <table border="1" style="width:100%;"> <tr> <th>Title</th> <th>Artist</th> <th>Country</th> <th>Company</th> <th>Price</th> <th>Year</th> </tr> ${esc.h}foreach(${esc.d}cd in ${esc.d}myXML.children().iterator()) ${esc.h}set(${esc.d}cdXML = ${esc.d}xmltool.parse(${esc.d}cd)) ${esc.h}if(${esc.d}velocityCount <= 5) <tr> <td>${esc.d}cdXML.TITLE.text </td> <td>${esc.d}cdXML.ARTIST.text </td> <td>${esc.d}cdXML.COUNTRY.text </td> <td>${esc.d}cdXML.COMPANY.text </td> <td>${esc.d}cdXML.PRICE.text </td> <td>${esc.d}cdXML.YEAR.text</td> </tr> ${esc.h}end ${esc.h}end </table>
Example Output#
Below is a live version of the code shown above, pulling from the "CD Catalog" XML document from W3Schools:
#set($myXML = $xmltool.read("https://www.w3schools.com/XML/cd_catalog.xml"))
#foreach($cd in $myXML.children().iterator()) #set($cdXML = $xmltool.parse($cd)) #if($velocityCount <= 5) #end #endTitle | Artist | Country | Company | Price | Year |
---|---|---|---|---|---|
$cdXML.TITLE.text | $cdXML.ARTIST.text | $cdXML.COUNTRY.text | $cdXML.COMPANY.text | $cdXML.PRICE.text | $cdXML.YEAR.text |