Velocity API FeedBlock to xml

John's Avatar

John

Jan 27, 2016 @ 09:18 PM

This question was posted a couple years ago: http://help.hannonhill.com/discussions/velocity-formats/8506-locator-tool-with-feed-block

I was wondering if there has been anything added to cascade that will allow me to convert the string version of the feed into xml searchable by xpath.

Thanks

  1. 1 Posted by Ryan Griffith on Jan 28, 2016 @ 01:23 PM

    Ryan Griffith's Avatar

    Hi John,

    Currently, we do not provide a helper method to return the feed's contents as XML to be parsed. I was able to locate the related suggestion on our Idea Exchange. I highly recommend voting this up if you would like for it to be considered for a future release.

    That being said, with a little bit of Java magic, I was able to come up with a workaround and successfully convert the XML string into a Document, which can then be used to obtain the root Element and be parsed by the XPath Tool:

    #set ( $feedblock = $_.locateBlock("/path/to/feed/block"))
    
    ## Generate a new StringReader instance using the feed's content.
    #set ( $StringReader = $_.getClass().forName("java.io.StringReader").getConstructors()[0].newInstance($feedblock.feed) )
    
    ## Create a new SAXBuilder instance which will be used to convert the StringBuilder to a Document.
    #set ( $SAXBuilder = $_.getClass().forName("org.jdom.input.SAXBuilder").newInstance() )
    
    ## Obtain the root Element from the built Document.
    #set ( $rootElement = $SAXBuilder.build($StringReader).rootElement )
    
    ## Using the root Element as context, obtain the items in the feed and loop over them
    #set ($items = $_XPathTool.selectNodes($rootElement, "//item"))
    #foreach ( $item in $items )
        $item.getChild("link").value
    #end
    
  2. 2 Posted by John on Jan 29, 2016 @ 03:35 PM

    John's Avatar

    Wow! Thanks a lot. Does exactly what I needed.

  3. 3 Posted by Ryan Griffith on Jan 29, 2016 @ 05:15 PM

    Ryan Griffith's Avatar

    Not a problem at all, John. Definitely a little hacky, but I am glad to hear the proposed code did the trick.

    I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  4. Ryan Griffith closed this discussion on Jan 29, 2016 @ 05:15 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac