Package core.apiCore.helpers
Class XmlHelper
- java.lang.Object
-
- core.apiCore.helpers.XmlHelper
-
public class XmlHelper extends Object
-
-
Constructor Summary
Constructors Constructor Description XmlHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static DocumentcleanNameSpace(Document doc)remove namespaces from xml gets namespace tags in list and removes themstatic StringconvertDocumentToString(Document doc)Convert a contents of a Document to a Stringstatic StringconvertXmlFileToString(Path filePath)Convert contents of an XML file to a stringstatic DocumentconvertXmlStringToDocument(String xmlStr)Convert a String in XML format to a Documentstatic NodeListgetNodeList(String xpathString, String xmlString)Get text value of the node specified by xpathstatic StringgetNodeValue(String xpathString, String xmlString)Get text value of the node specified by xpathstatic StringgetRequestBodyFromXmlTemplate(ServiceObject serviceObject)if request body is empty, return xml template string if request body contains xml tag, replace tag with value eg.static StringgetXmlTagValue(String value, String tag)get first matching tag valuestatic StringgetXmlTagValue(String source, String tag, int position)get tag valuestatic StringgetXpathFromXml(String xml, String xpath)get xpath value form xml string test out xpath values at: https://www.freeformatter.com/xpath-tester.html#ad-outputstatic StringgetXpathValueFromJson(String json, String xpath)converts json string to xml and gets xpath value from itstatic booleanisValidXmlString(String xmlString)validates if xml string is valid xmlstatic booleanisXmlFile(String filename)return true if file is xml filestatic StringjsonToXml(String json)convert json string to xml stringstatic StringprettyFormat(String input, int indent)static DocumentreadFile(String inputFilePath)Read a XML file And get a Document retrying And catching interruptions from other threads.static StringremoveXmlNameSpace(String xmlString)remove namespaces from xml gets namespace tags in list and removes themstatic StringreplaceGroup(String regex, String source, int groupToReplace, int groupOccurrence, String replacement)generic replace value using regex using indexstatic StringreplaceRequestTagValues(String requestbody, String xmlString)replaces request header tag values eg.static StringreplaceTagValue(String xml, String tag, String value)replace tag value in xml for all occurrencesstatic StringreplaceTagValue(String xml, String tag, String value, int position)replace tag value in xml string at index
-
-
-
Method Detail
-
convertDocumentToString
public static String convertDocumentToString(Document doc)
Convert a contents of a Document to a String- Parameters:
doc-- Returns:
- String
-
convertXmlStringToDocument
public static Document convertXmlStringToDocument(String xmlStr)
Convert a String in XML format to a Document- Parameters:
xmlStr-- Returns:
- Document
-
convertXmlFileToString
public static String convertXmlFileToString(Path filePath)
Convert contents of an XML file to a string- Parameters:
filePath-- Returns:
- String
-
readFile
public static Document readFile(String inputFilePath)
Read a XML file And get a Document retrying And catching interruptions from other threads.- Parameters:
inputFilePath-- Returns:
- Document
-
getNodeValue
public static String getNodeValue(String xpathString, String xmlString)
Get text value of the node specified by xpath- Parameters:
xpathString-xmlString-- Returns:
- String
-
getNodeList
public static NodeList getNodeList(String xpathString, String xmlString)
Get text value of the node specified by xpath- Parameters:
xpathString-xmlString-- Returns:
- String
-
getXpathValueFromJson
public static String getXpathValueFromJson(String json, String xpath)
converts json string to xml and gets xpath value from it- Parameters:
json-xpath-- Returns:
- result list as string separated by ","
-
getXpathFromXml
public static String getXpathFromXml(String xml, String xpath)
get xpath value form xml string test out xpath values at: https://www.freeformatter.com/xpath-tester.html#ad-output- Parameters:
xml-xpath-- Returns:
- result list as string separated by ","
-
jsonToXml
public static String jsonToXml(String json)
convert json string to xml string- Parameters:
json-- Returns:
-
getRequestBodyFromXmlTemplate
public static String getRequestBodyFromXmlTemplate(ServiceObject serviceObject)
if request body is empty, return xml template string if request body contains xml tag, replace tag with value eg. "soi:EquipmentID:1:equip_<@_TIME_17>"- Parameters:
serviceObject-- Returns:
-
replaceTagValue
public static String replaceTagValue(String xml, String tag, String value, int position)
replace tag value in xml string at index- Parameters:
position- : starts at 1tag-value-- Returns:
-
replaceTagValue
public static String replaceTagValue(String xml, String tag, String value)
replace tag value in xml for all occurrences- Parameters:
xml-tag-value-- Returns:
-
replaceGroup
public static String replaceGroup(String regex, String source, int groupToReplace, int groupOccurrence, String replacement)
generic replace value using regex using index- Parameters:
regex-source-groupToReplace-groupOccurrence-replacement-- Returns:
-
replaceRequestTagValues
public static String replaceRequestTagValues(String requestbody, String xmlString)
replaces request header tag values eg. "soi:EquipmentID:1:<@_TIME_16>" tag: soi:EquipmentID, position: 1, value: <@_TIME_16>- Parameters:
serviceObject-- Returns:
-
getXmlTagValue
public static String getXmlTagValue(String value, String tag)
get first matching tag value- Parameters:
requestBody-tag-- Returns:
-
getXmlTagValue
public static String getXmlTagValue(String source, String tag, int position)
get tag value- Parameters:
source-tag-position- : starts at 1- Returns:
-
isXmlFile
public static boolean isXmlFile(String filename)
return true if file is xml file- Parameters:
filename-- Returns:
-
isValidXmlString
public static boolean isValidXmlString(String xmlString)
validates if xml string is valid xml- Parameters:
xmlString-- Returns:
-
removeXmlNameSpace
public static String removeXmlNameSpace(String xmlString)
remove namespaces from xml gets namespace tags in list and removes them- Parameters:
xmlString-- Returns:
-
-