-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for formatting XML and JSON request and response
- Loading branch information
Akshay Sharma
committed
Oct 20, 2023
1 parent
a6ac456
commit 4e6087c
Showing
2 changed files
with
61 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.soapuiextentter.utilities; | ||
|
||
import javax.xml.parsers.DocumentBuilderFactory; | ||
|
||
import org.json.JSONObject; | ||
import org.w3c.dom.bootstrap.DOMImplementationRegistry; | ||
import org.w3c.dom.ls.DOMImplementationLS; | ||
import org.w3c.dom.ls.LSSerializer; | ||
import org.xml.sax.InputSource; | ||
|
||
public class StringUtils { | ||
|
||
public static String prettyPrintXML(String xmlString) { | ||
String preetyXML = xmlString; | ||
try { | ||
InputSource src = new InputSource(new String(xmlString)); | ||
org.w3c.dom.Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src) | ||
.getDocumentElement(); | ||
Boolean keepDeclaration = Boolean.valueOf(xmlString.startsWith("<?xml")); | ||
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); | ||
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); | ||
LSSerializer writer = impl.createLSSerializer(); | ||
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); | ||
writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); | ||
preetyXML = writer.writeToString(document); | ||
} catch (Exception e) { | ||
return preetyXML; | ||
} | ||
return preetyXML; | ||
} | ||
|
||
public static String prettyPrintJson(String jsonString) { | ||
String preetyJson = jsonString; | ||
try { | ||
preetyJson = (new JSONObject(jsonString)).toString(4); | ||
} catch (Exception e) { | ||
return preetyJson; | ||
} | ||
return preetyJson; | ||
} | ||
} |