Service Virtualization

Expand all | Collapse all

Format XML Payoad which is in single line

  • 1.  Format XML Payoad which is in single line

    Posted 12-16-2018 07:12 PM
      |   view attached

    Hi Team,

    We have a requirement to format the XML before sending to another application. Kindly let us know how we can achieve this.

     

    Request Payload:

    In a single line.

    <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> <Request> <ns0:ebXMLDetails xmlns:ns0="http://www.tibco.com/schemas/LISA_BW/Schemas/Schema.xsd"> <ns0:ebXMLService>Manage Appointment</ns0:ebXMLService> <ns0:ebXMLOperation>requestAppointmentAvailability</ns0:ebXMLOperation> <ns0:ebXMLConversationID>ID:T8101-ULTIMO-EMS-SERVER-1.70F15BC65C245A854A:999__ISL-FWOM__20181213171857000012</ns0:ebXMLConversationID> <ns0:ebXMLVersionID>v2.0</ns0:ebXMLVersionID> <ns0:ebXMLMessageID>20181213-061858-43870@172.17.0.2</ns0:ebXMLMessageID> </ns0:ebXMLDetails> <v4:ManageAppointmentRequest xmlns:v3="http://www.nbnco.com.au/cim/common/place/v3" xmlns:v4="http://www.nbnco.com.au/cim/manageAppointment/v4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <AppointmentSearch> <BusinessInteractionAppointment> <DescribedBy> <value>No</value> <Characteristic> <ID>Priority Assist</ID> </Characteristic> </DescribedBy> <DescribedBy> <value>Standard</value> <Characteristic> <ID>AppointmentSLA</ID> </Characteristic> </DescribedBy> <SpecifiedBy> <name>Standard Install</name> <type>Demand Type</type> </SpecifiedBy> <InvolvesPlace xsi:type="v3:NBNLocation"> <ID>LOC180615040230</ID> </InvolvesPlace> <InvolvesAppointmentSlot> <appointmentDate> <startDateTime>2018-12-29T00:00:00</startDateTime> <endDateTime>2019-01-11T00:00:00</endDateTime> </appointmentDate> </InvolvesAppointmentSlot> </BusinessInteractionAppointment> </AppointmentSearch> </v4:ManageAppointmentRequest> </Request> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

     

    Requirement to format as below

     

    Attachment(s)

    docx
    Format XML Payload.docx   111K 1 version


  • 2.  Re: Format XML Payoad which is in single line

    Posted 12-23-2018 07:27 AM

    Hi,

     

    There is no step and/or public function and/or public method available within DevTest AFAIK. So, within an "Execute Script" step you would be able to do this with some java coding, and in that case StackOverflow often becomes your best friend (as it was mine for the below).

     

    So try below code. There is one caveat: your source XML contains whitespace between the elements, and the XML transformer did not output the xml tags on different lines until I removed that whitespace. That's why the first action is a replaceAll() call. BUT, as it is right now this for any xml element that has only spaces as content, this line would also remove those. So an element like <someTag>          <someTag> would become <someTag><someTag>

     

     

    After the script runs the formatted xml would be stored in a property called "formattedXML"

     

    import java.io.StringWriter;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import java.io.StringReader;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.xml.sax.InputSource;

     

    String sourceXml = lisa_vse_request.getBodyText();

     

    sourceXml = sourceXml.replaceAll(">\\s++<", "><");

    //Parser that produces DOM object trees from XML content
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    //API to obtain DOM Document instance
    DocumentBuilder builder = null;
    //Create DocumentBuilder with default configuration
    builder = factory.newDocumentBuilder();
    //Parse the content to Document object
    Document doc = builder.parse(new InputSource(new StringReader(sourceXml)));

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    //initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    String xmlString = result.getWriter().toString();

     

    testExec.setStateValue("formattedXML", xmlString)

     

    Hope this helps.

     

    Cheers,

    Danny