external xml feed with multiple elements

lauren.fraser's Avatar

lauren.fraser

28 Mar, 2016 07:39 PM

I have an external xml feed that looks like this:

   <Room property_id="1" room_id="19" room_name="BTC Conf Rm 103">
        <booking event_id="32">
            <Start_Time>3/28/2016 8:00:00 AM</Start_Time>
            <End_Time>3/28/2016 5:00:00 PM</End_Time>
            <Setup_Time>3/28/2016 7:30:00 AM</Setup_Time>
            <Break_Down_Time>3/28/2016 5:30:00 PM</Break_Down_Time>
            <Host>HUMAN RESOURCES</Host>
            <Name_Of_Meeting>GENERAL ORIENTATION</Name_Of_Meeting>
        </booking>
    </Room>

I need to be able to access the individual elements in the Room tag: room_id and room_name.

I'm not sure how to define these in velocity. Any suggestions?
Thanks,
Lauren

  1. 1 Posted by Ryan Griffith on 28 Mar, 2016 07:58 PM

    Ryan Griffith's Avatar

    Hi Lauren,

    When you obtain the Room element, you can access the element's attributes using $roomElement.getAttribute("room_id").value.

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by lauren.fraser on 28 Mar, 2016 09:11 PM

    lauren.fraser's Avatar

    Ryan,
    I’m not sure what I’m doing wrong. I’m not getting any errors and not getting any content output:

    #set ($feed = $_XPathTool.selectSingleNode($contentRoot, "/Rooms") )
    #set ($events = $feed.getChild("Room"))
    #set ($bookings = $events.getChild("booking"))

    #foreach
        ($booking in $bookings)
        #set ($startTime = $booking.getChild("Start_Time"))
        #set ($endTime = $booking.getChild("End_Time").value)
        #set ($host = $boooking.getChild("Host").value)
        #set ($meetingName = $booking.getChild("Name_Of_Meeting").value)
        #set ($roomID = $booking.getAttribute("room_id").value)
        #set ($roomName = $booking.getAttribute("room_name").value)

    <p> $_EscapeTool.xml($startTime.value)</p>

    #end

  3. 3 Posted by Ryan Griffith on 29 Mar, 2016 12:08 PM

    Ryan Griffith's Avatar

    Hi Lauren,

    Your $feed variable will already be an array of Room elements, so you can remove the next line. I would adjust to the following:

    #set ($room = $_XPathTool.selectSingleNode($contentRoot, "/Room") ) 
    #set ($bookings = $_XPathTool.selectNodes($room, "booking"))
    
    #if ($bookings.size() > 0)
        #foreach ($booking in $bookings) 
            ...
        #end
    #end
    

    Note: this assumes the XML structure you provided in your original comment. If you do happen to have that additional Rooms element, I would suggest two loops for both the rooms and their bookings.

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by lauren.fraser on 29 Mar, 2016 03:32 PM

    lauren.fraser's Avatar

    Ryan,
    The xml structure should stay the same. I tried this xml and I'm still not getting any values.

    #set ($room = $_XPathTool.selectSingleNode($contentRoot, "/Room") )
    #set ($bookings = $room.getChild("booking"))
    #if ($bookings.size() > 0)
        #foreach ($booking in $bookings)
        #set ($startTime = $booking.getChild("Start_Time"))
        #set ($endTime = $booking.getChild("End_Time"))
        #set ($host = $booking.getChild("Host"))
        #set ($meetingName = $booking.getChild("Name_Of_Meeting"))
        
        #set ($roomID = $room.getAttribute("room_id").value) 
        #set ($roomName = $room.getAttribute("room_name").value)
    
                <p> $_EscapeTool.xml($startTime.value)</p>
                <p>$_EscapeTool.xml($roomID)</p>
        #end
    #end
    
  5. 5 Posted by Ryan Griffith on 29 Mar, 2016 06:19 PM

    Ryan Griffith's Avatar

    Lauren,

    To confirm, will you always have a single Room with multiple booking elements? If so, perhaps the XPath on the first line is incorrect depending on how you are applying it to the format.

    When you have a moment, try changing:

    #set ($room = $_XPathTool.selectSingleNode($contentRoot, "/Room") )
    

    to:

    #set ($room = $_XPathTool.selectSingleNode($contentRoot, "//Room") )
    

    Please let me know if you have any questions.

    Thanks!

  6. 6 Posted by lauren.fraser on 29 Mar, 2016 07:29 PM

    lauren.fraser's Avatar

    Ryan,
    Same result: No errors, no data.

  7. 7 Posted by Ryan Griffith on 29 Mar, 2016 07:43 PM

    Ryan Griffith's Avatar

    Thank you for trying, Lauren.

    When you have a moment, please paste or attach sample XML that you are applying to this format so I can take a closer look.

    Please let me know if you have any questions.

    Thanks!

  8. 8 Posted by Wing Ming Chan on 29 Mar, 2016 07:47 PM

    Wing Ming Chan's Avatar
    Room ID: $contentRoot.getAttribute( "room_id" ).value<br />
    Room Name: $contentRoot.getAttribute( "room_name" ).value<br />
    
    #set( $events   = $contentRoot.getChild( "Room" ) )
    #set( $bookings = $contentRoot.getChildren( "booking" ) )
    
    #if ($bookings.size() > 0)
        #foreach( $booking in $bookings )
            #set( $startTime   = $booking.getChild( "Start_Time" ).value )
            #set( $endTime     = $booking.getChild( "End_Time" ).value )
            #set( $host        = $boooking.getChild( "Host" ).value )
            #set( $meetingName = $booking.getChild( "Name_Of_Meeting" ).value )
            #set( $roomID      = $booking.getAttribute( "room_id" ).value )
            #set( $roomName    = $booking.getAttribute( "room_name" ).value )
    
    <p> $_EscapeTool.xml( $startTime )</p>
        #end
    #end
    

    Wing

  9. 9 Posted by Ryan Griffith on 29 Mar, 2016 07:55 PM

    Ryan Griffith's Avatar

    Thanks for chiming in Wing.

    $contentRoot.getAttribute()

    This will depend on how the XML is being applied to the Format (i.e. direct region assignment or an Index Block).

  10. 10 Posted by lauren.fraser on 29 Mar, 2016 07:57 PM

    lauren.fraser's Avatar

    Here it is:

    <Rooms Time_Generated="3/29/2016 2:20:00 PM">
        <Room property_id="1" room_id="20" room_name="BTC Conf Rm LL-4">
            <booking event_id="505">
                <Start_Time>3/29/2016 8:00:00 AM</Start_Time>
                <End_Time>3/29/2016 5:00:00 PM</End_Time>
                <Setup_Time>3/29/2016 7:30:00 AM</Setup_Time>
                <Break_Down_Time>3/29/2016 5:30:00 PM</Break_Down_Time>
                <Host>LAB</Host>
                <Name_Of_Meeting>LAB TRAINING</Name_Of_Meeting>
            </booking>
        </Room>
        <Room property_id="1" room_id="3" room_name="CR 2">
            <booking event_id="693">
                <Start_Time>3/29/2016 1:00:00 PM</Start_Time>
                <End_Time>3/29/2016 2:30:00 PM</End_Time>
                <Setup_Time>3/29/2016 12:30:00 PM</Setup_Time>
                <Break_Down_Time>3/29/2016 3:00:00 PM</Break_Down_Time>
                <Host>HUMAN RESOURCES</Host>
                <Name_Of_Meeting>HIRING EVENT FOLLOWUP</Name_Of_Meeting>
            </booking>
            <booking event_id="16">
                <Start_Time>3/29/2016 3:30:00 PM</Start_Time>
                <End_Time>3/29/2016 11:55:00 PM</End_Time>
                <Setup_Time>3/29/2016 3:00:00 PM</Setup_Time>
                <Break_Down_Time>3/30/2016 12:25:00 AM</Break_Down_Time>
                <Host>MARKETING</Host>
                <Name_Of_Meeting>SET-UP -- DOCTORS APPRECIATION</Name_Of_Meeting>
            </booking>
        </Room>
        <Room property_id="1" room_id="4" room_name="CR 3">
            <booking event_id="679">
                <Start_Time>3/29/2016 1:30:00 PM</Start_Time>
                <End_Time>3/29/2016 2:30:00 PM</End_Time>
                <Setup_Time>3/29/2016 1:00:00 PM</Setup_Time>
                <Break_Down_Time>3/29/2016 3:00:00 PM</Break_Down_Time>
                <Host>CODING</Host>
                <Name_Of_Meeting>LABCORP CODING ISSUES</Name_Of_Meeting>
            </booking>
            <booking event_id="16">
                <Start_Time>3/29/2016 3:30:00 PM</Start_Time>
                <End_Time>3/29/2016 11:55:00 PM</End_Time>
                <Setup_Time>3/29/2016 3:00:00 PM</Setup_Time>
                <Break_Down_Time>3/30/2016 12:25:00 AM</Break_Down_Time>
                <Host>MARKETING</Host>
                <Name_Of_Meeting>SET-UP -- DOCTORS APPRECIATION</Name_Of_Meeting>
            </booking>
        </Room>
        <Room property_id="1" room_id="5" room_name="**PDR**">
            <booking event_id="689">
                <Start_Time>3/29/2016 3:00:00 PM</Start_Time>
                <End_Time>3/29/2016 4:00:00 PM</End_Time>
                <Setup_Time>3/29/2016 2:30:00 PM</Setup_Time>
                <Break_Down_Time>3/29/2016 4:30:00 PM</Break_Down_Time>
                <Host>PERFORMANCE IMPROVEMENT</Host>
                <Name_Of_Meeting>PI REPORTING MTG</Name_Of_Meeting>
            </booking>
            <booking event_id="192">
                <Start_Time>3/29/2016 6:30:00 PM</Start_Time>
                <End_Time>3/29/2016 8:30:00 PM</End_Time>
                <Setup_Time>3/29/2016 6:00:00 PM</Setup_Time>
                <Break_Down_Time>3/29/2016 9:00:00 PM</Break_Down_Time>
                <Host>BIRTH PLACE</Host>
                <Name_Of_Meeting>PRENATAL CLASSES</Name_Of_Meeting>
            </booking>
        </Room>
        <Room property_id="1" room_id="19" room_name="BTC Conf Rm 103">
            <booking event_id="658">
                <Start_Time>3/29/2016 3:30:00 PM</Start_Time>
                <End_Time>3/29/2016 5:00:00 PM</End_Time>
                <Setup_Time>3/29/2016 3:00:00 PM</Setup_Time>
                <Break_Down_Time>3/29/2016 5:30:00 PM</Break_Down_Time>
                <Host>SURGERY - INPATIENT &amp; OUTPATIENT</Host>
                <Name_Of_Meeting>SURGERY STAFF MTG</Name_Of_Meeting>
            </booking>
        </Room>
        <Room property_id="1" room_id="2" room_name="CR 1">
            <booking event_id="16">
                <Start_Time>3/29/2016 3:30:00 PM</Start_Time>
                <End_Time>3/29/2016 11:55:00 PM</End_Time>
                <Setup_Time>3/29/2016 3:00:00 PM</Setup_Time>
                <Break_Down_Time>3/30/2016 12:25:00 AM</Break_Down_Time>
                <Host>MARKETING</Host>
                <Name_Of_Meeting>SET-UP -- DOCTORS APPRECIATION</Name_Of_Meeting>
            </booking>
        </Room>
    </Rooms>
    
  11. 11 Posted by lauren.fraser on 29 Mar, 2016 07:58 PM

    lauren.fraser's Avatar

    Ryan, I have this set up in a feed block. My plan was to apply that feed block to the content region and apply the format. I did try Wing's code, with the same result: no error, no data.

     #set ($roomID = $room.getAttribute("room_id").value) 
        #set ($roomName = $room.getAttribute("room_name").value)
    
                <p> $_EscapeTool.xml($startTime.value)</p>
                <p>$_EscapeTool.xml($roomID)</p>
    
  12. 12 Posted by Ryan Griffith on 29 Mar, 2016 08:07 PM

    Ryan Griffith's Avatar

    Hi Lauren,

    I suspected there may have been a discrepancy with the XML. When you have a moment, let's try the following:

    #set ($rooms = $_XPathTool.selectNodes($contentRoot, "//Room") )
    #if ($rooms.size() > 0)
        #foreach ($room in $rooms)
          #set ($roomID = $room.getAttribute("room_id").value) 
          #set ($roomName = $room.getAttribute("room_name").value)
    
          <p>$_EscapeTool.xml($roomID)</p>
          
          #set ($bookings = $room.getChildren("booking"))
          #if ($bookings.size() > 0)
            #foreach ($booking in $bookings)
                #set ($startTime = $booking.getChild("Start_Time"))
                #set ($endTime = $booking.getChild("End_Time"))
                #set ($host = $booking.getChild("Host"))
                #set ($meetingName = $booking.getChild("Name_Of_Meeting"))
        
                <p> $_EscapeTool.xml($startTime.value)</p>
            #end
          #end
      #end
    #end
    

    Please let me know if you have any questions.

    Thanks!

  13. 13 Posted by lauren.fraser on 29 Mar, 2016 08:14 PM

    lauren.fraser's Avatar

    Ryan,
    That worked. I’ve got some more sorting, etc to work on, but now I’m on the right track. Thanks for all the help!

  14. 14 Posted by Ryan Griffith on 29 Mar, 2016 08:16 PM

    Ryan Griffith's Avatar

    Not a problem at all, Lauren. Glad to hear the latest changes 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!

  15. Ryan Griffith closed this discussion on 29 Mar, 2016 08:16 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

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM