This topic contains 1 reply, has 2 voices, and was last updated by  Casey Trauer 4 weeks, 1 day ago.

Unable to split child nodes

  • Hi,

    I have a requirement where my client is sending data over HTTP Api in the format

    XMLMsg=<Parent>
    <Child>
    <requestId>1</requestId>
    <appointmentDate>2018-09-21</appointmentDate>
    <Child>
    <Child>
    <requestId>2</requestId>
    <appointmentDate>2018-09-21</appointmentDate>
    <Child>
    </Parent>

    I need to send this XML data to our webservice in the same XML format.

    So for mapping I created a template

    template= <appointment>
    <requestId></requestId>
    <appointmentDate></appointmentDate>
    </appointment>

    During mapping I am using setinner to set the value of template with the incoming XMLMsg. like below
    local traverse(template,XMLMsg)
    for i=1, XMLMsg.appointments:childCount(‘appointment’) do

    Xml.appointment.requestId:setInner(XMLMsg.appointments.appointment.requestId:nodeText())
    Xml.appointment.appointmentDate:setInner(XMLMsg.appointments.appointment.appointmentDate:nodeText())

    end
    return Xml

    And then I format the result like below.

    function formatResult(Xml)

    — If XML, then concatenate results with table.concat
    local Output = “<appointments>\n”..table.concat(Xml)..”\n</appointments>”
    trace(Output)
    return Output, ‘text/xml’
    end

    But when I checked the return output from formatresult, I am getting

    <appointments>
    <appointment>
    <requestId>1</requestId>
    <appointmentDate>2018-09-21</appointmentDate>
    </appointment><appointment>
    <requestId>1</requestId>
    <appointmentDate>2018-09-21</appointmentDate>
    </appointment>
    </appointments>

    I am unable to split the childs “</appointment><appointment>” and should be separated by a newline. Can someone help me on how to achieve this.

    Hello,

    I am reading two core challenges here:
    1. You are mapping the values from one node over and over.
    2. You are not happy with the formatting of the xml.

    The first thing I would try is setting an index in your for loop:

    Xml.appointment.requestId:setInner(XMLMsg.appointments.appointment[i].requestId:nodeText())

    As for the formatting, check the documentation for the table.concat function. You can pass a separator into the function, probably like this:

    table.concat(Xml,'/n')

    Hope that helps.

    Casey Trauer,
    Director, Client Education
    iNTERFACEWARE

You must be logged in to reply to this topic.